﻿using MTHHelpers;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
using EasyModbus;
using S7.Net;

namespace _8轴运动控制
{

    public enum RunStep
    {
        //等待运行
        RunReady,

        //1轴/2轴到达盘正中心/拍照（小端面）
        Move01ToCenter,

        //1轴/2轴到达盘挖槽中心/拍照
        Move01ToGroove,

        //3轴/4轴/5轴到达盘大外圆侧/开光源/拍照/关光源
        Move345ToLarge,

        //6轴刹车盘转动一定角度
        Move6Rotate1,

        //3轴/4轴/5轴到达盘小端侧壁/开光源/拍照/
        Move345ToSmall,

        //6轴刹车盘转动一定角度
        Move6Rotate2,

        //1轴/2轴回到安全位置
        Move01ToSafe,

        //气缸夹紧刹车盘，7轴上升一段距离
        Move7ToUp,

        //8轴翻转180度
        Move8Flip,

        //7轴下降一定高度气缸打开
        Move7ToDown,

        //1轴/2轴到达盘正中心/拍照（内腔）
        Move01ToNeiQiang,

        //1轴/2轴到达盘下刹面/拍照 +3轴/4轴/5轴到达盘内腔侧壁/拍照
        Move01234BrakeAndNeiqiang,

        //6轴刹车盘转动一定角度
        Move6Rotate3,

        //1/2/3/4/5轴回安全位置，取图完成
        Move01234ToSafe,

        //总步骤
        MaxStep,
    }

    public partial class FrmMonitor : Form
    {
        public FrmMonitor()
        {
            InitializeComponent();
        }

        #region 变量创建
        //轴数量
        float AxisNum = 0;
        //上升/下降沿变量
        GetPN getPN = new GetPN();
        //表示系统模式，True表示自动模式，False表示手动模式
        private bool AutoMode = false;
        //自动流程是否开启判断
        private bool isAutoTaskRunning = false;
        //自动流程是否开启判断
        private bool isManuTaskRunning = false;
        //轴卡连接是否成功判断
        private bool isConnectOK = false;
        private Zmotion zmotion = new Zmotion();
        private short upAxis0 = 0;
        private short upAxis1 = 1;
        private short sideAxis1 = 2;
        private short sideAxis2 = 3;
        private short sideAxis3 = 4;
        private short rotateAxis = 5;
        private short updownAxis = 6;
        private short flipAxis = 7;
        //单轴运动，选择轴号
        private short Axis
        {
            get
            {
                short axis = this.rdo_axis0.Checked ? upAxis0 : this.rdo_axis1.Checked ? upAxis1 : this.rdo_axis2.Checked ? sideAxis1
                  : this.rdo_axis3.Checked ? sideAxis2 : this.rdo_axis4.Checked ? sideAxis3 : this.rdo_axis5.Checked ? rotateAxis
                  : this.rdo_axis6.Checked ? updownAxis : this.rdo_axis7.Checked ? flipAxis : (short)-1;
                if (axis == -1)
                {
                    MessageBox.Show("请选择轴");
                    return axis;
                }
                return axis;
            }
        }

        //单轴运动，选择移动距离
        private float Distance
        {
            get
            {
                return this.rdo_axis0.Checked ? Convert.ToSingle(this.num_dis0.Value)
                    : this.rdo_axis1.Checked ? Convert.ToSingle(this.num_dis1.Value)
                    : this.rdo_axis2.Checked ? Convert.ToSingle(this.num_dis2.Value)
                    : this.rdo_axis3.Checked ? Convert.ToSingle(this.num_dis3.Value)
                    : this.rdo_axis4.Checked ? Convert.ToSingle(this.num_dis4.Value)
                    : this.rdo_axis5.Checked ? Convert.ToSingle(this.num_dis5.Value)
                    : this.rdo_axis6.Checked ? Convert.ToSingle(this.num_dis6.Value)
                     : Convert.ToSingle(this.num_dis7.Value);
            }
        }

        #region  速度定义
        //调试用速度
        private float velMax = 0.0f;
        private float velMin = 0.0f;
        private float acc = 0.0f;
        private float dec = 0.0f;
        private float sramp = 0.0f;
        //自动运行时速度
        //插补运动 1轴 
        private float unit0 = 0.0f;
        private float velMax0 = 0.0f;
        private float velMin0 = 0.0f;
        private float acc0 = 0.0f;
        private float dec0 = 0.0f;
        private float sramp0 = 0.0f;
        //2轴速度
        private float unit1 = 0.0f;
        private float velMax1 = 0.0f;
        private float velMin1 = 0.0f;
        private float acc1 = 0.0f;
        private float dec1 = 0.0f;
        private float sramp1 = 0.0f;
        //插补运动 3轴 速度
        private float unit2 = 0.0f;
        private float velMax2 = 0.0f;
        private float velMin2 = 0.0f;
        private float acc2 = 0.0f;
        private float dec2 = 0.0f;
        private float sramp2 = 0.0f;
        //4轴 速度
        private float unit3 = 0.0f;
        private float velMax3 = 0.0f;
        private float velMin3 = 0.0f;
        private float acc3 = 0.0f;
        private float dec3 = 0.0f;
        private float sramp3 = 0.0f;
        //5轴速度
        private float unit4 = 0.0f;
        private float velMax4 = 0.0f;
        private float velMin4 = 0.0f;
        private float acc4 = 0.0f;
        private float dec4 = 0.0f;
        private float sramp4 = 0.0f;
        //绝对运动 6轴速度
        private float unit5 = 0.0f;
        private float velMax5 = 0.0f;
        private float velMin5 = 0.0f;
        private float acc5 = 0.0f;
        private float dec5 = 0.0f;
        private float sramp5 = 0.0f;
        //绝对运动 7轴速度
        private float unit6 = 0.0f;
        private float velMax6 = 0.0f;
        private float velMin6 = 0.0f;
        private float acc6 = 0.0f;
        private float dec6 = 0.0f;
        private float sramp6 = 0.0f;
        //绝对运动 8轴速度
        private float unit7 = 0.0f;
        private float velMax7 = 0.0f;
        private float velMin7 = 0.0f;
        private float acc7 = 0.0f;
        private float dec7 = 0.0f;
        private float sramp7 = 0.0f;

        #endregion

        #region 基准位置读取
        //上面相机
        private float Axis0Base = 0.0f;
        private float Axis0Safe = 0.0f;
        private float Axis1Base = 0.0f;
        private float Axis1Safe = 0.0f;
        private float Axis0limitUp = 0.0f;
        private float Axis0limitDown = 0.0f;
        private float Axis1limitOut = 0.0f;
        private float Axis1limitIn = 0.0f;
        //侧面相机
        private float Axis2Base = 0.0f;
        private float Axis2Safe = 0.0f;
        private float Axis3Base = 0.0f;
        private float Axis3Baseqx = 0.0f;
        private float Axis3Safe = 0.0f;
        private float Axis4Base1 = 0.0f;
        private float Axis4Baseqx = 0.0f;
        private float Axis4Safe = 0.0f;
        private float Axis2limitUp = 0.0f;
        private float Axis2limitDown = 0.0f;
        private float Axis3limitOut = 0.0f;
        private float Axis3limitIn = 0.0f;
        //旋转轴
        private float Axis5Base = 0.0f;
        //上升轴
        private float Axis6Wait = 0.0f;
        private float Axis6GripBase = 0.0f;
        private float Axis6Up = 0.0f;
        private float Axis6Down = 0.0f;
        //翻面轴
        private float Axis7Ppos = 0.0f;
        private float Axis7Npos = 0.0f;


        #endregion

        #region 工件信息
        float gongjiangaodu = 0;
        float xiaoduanmiantutai = 0;
        float xiashamiantutai = 0;
        float xiaoduangaodu = 0;
        float shahou = 0;
        float neiqiangshendu = 0;
        float xiaoduanbihou = 0;
        float xiaoduanzhijing = 0;
        float zhongkongzhijing = 0;
        float dawaiyuan = 0;
        float neiyuanzhijing = 0;
        float wacaozhijing = 0;
        float neiqiangzhijing = 0;
        #endregion

        //当前步骤
        private RunStep CurrentStep { get; set; } = RunStep.RunReady;

        //自动过程停止
        private bool IsAutoStop = false;

        //系统急停bool量
        private bool IsEmgStop = false;

        //暂停信号
        private bool IsPause = false;

        Dictionary<string, int> DI = new Dictionary<string, int>();
        Dictionary<string, int> DO = new Dictionary<string, int>();


        //仿真运行
        bool Simulate = false;

        //超出限位
        bool OutLimit = false;

        //点位示教登陆用
        bool b_con = false;
        //PLC设置登陆用
        bool b_con1 = false;

        //流程开始后，手动门开
        private bool IsContinue = false;

        //等待超时表示
        private bool WaitOutTime = false;
        Timer time = new Timer(5000);

        //系统复位
        private bool systemReset = false;

        #region PLC信息
        public Mitsubishi mc = new Mitsubishi();
        public SiemensS7 ms = new SiemensS7();
        string plcModel = null;
        string startMachine = null;
        string robotCall = null;
        string robotIn = null;
        string preFix = null;
        string model = null;
        string machineOK = null;
        string machineRun = null;
        string checkOK = null;
        string checkNG = null;
        string posOK = null;
        string posNG = null;
        string IPMitsu = null;
        string PortMitsu = null;
        string Heart = null;

        #endregion

        #endregion

        #region 控件事件
        private void FrmMonitor_Load(object sender, EventArgs e)
        {
            //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true);
            //this.SetStyle(ControlStyles.ResizeRedraw, true);
            //this.SetStyle(ControlStyles.Selectable, true);
            //路径初始化
            CommendMethods.SysSetPath = Application.StartupPath + "\\Settings\\SysSet.ini";
            CommendMethods.PosBasePath = Application.StartupPath + "\\Posteach\\PosBase.ini";
            CommendMethods.strFilePath = Application.StartupPath + "\\ECAT初始化16.bas";
            CommendMethods.PlcSetPath = Application.StartupPath + "\\PlcSettings\\PlcSet.ini";
            //隐藏tabcontrol的item标题 （实际不是隐藏，是设置的很小，人肉眼看不见）
            this.tab_Monitor.SizeMode = TabSizeMode.Fixed;
            this.tab_Monitor.ItemSize = new System.Drawing.Size(0, 1);
            time.Elapsed += WaitOut;
            //读取参数
            GetParam();
            //信号绑定
            DigitalSignal();
            //读取脉冲当量
            SetOnlyUnit();
            //读取基准值
            ReadBasePos();
            //读取三菱PLC信息
            ReadMitsubishi();
            #region //解析系统文件

            #region 解析系统文件
            //this.txt_IpAdress.Text = Convert.ToString(objConfig.IP_Adress);
            //运动参数
            this.num_unit0.Value = Convert.ToDecimal(unit0);
            this.num_velmin0.Value = Convert.ToDecimal(velMin0);
            this.num_velmax0.Value = Convert.ToDecimal(velMax0);
            this.num_acc0.Value = Convert.ToDecimal(acc0);
            this.num_dec0.Value = Convert.ToDecimal(dec0);
            this.num_sramp0.Value = Convert.ToDecimal(sramp0);

            this.num_unit1.Value = Convert.ToDecimal(unit1); ;
            this.num_velmin1.Value = Convert.ToDecimal(velMin1);
            this.num_velmax1.Value = Convert.ToDecimal(velMax1);
            this.num_acc1.Value = Convert.ToDecimal(acc1);
            this.num_dec1.Value = Convert.ToDecimal(dec1);
            this.num_sramp1.Value = Convert.ToDecimal(sramp1);

            this.num_unit2.Value = Convert.ToDecimal(unit2); ;
            this.num_velmin2.Value = Convert.ToDecimal(velMin2);
            this.num_velmax2.Value = Convert.ToDecimal(velMax2);
            this.num_acc2.Value = Convert.ToDecimal(acc2);
            this.num_dec2.Value = Convert.ToDecimal(dec2);
            this.num_sramp2.Value = Convert.ToDecimal(sramp2);

            this.num_unit3.Value = Convert.ToDecimal(unit3); ;
            this.num_velmin3.Value = Convert.ToDecimal(velMin3);
            this.num_velmax3.Value = Convert.ToDecimal(velMax3);
            this.num_acc3.Value = Convert.ToDecimal(acc3);
            this.num_dec3.Value = Convert.ToDecimal(dec3);
            this.num_sramp3.Value = Convert.ToDecimal(sramp3);

            this.num_unit4.Value = Convert.ToDecimal(unit4);
            this.num_velmin4.Value = Convert.ToDecimal(velMin4);
            this.num_velmax4.Value = Convert.ToDecimal(velMax4);
            this.num_acc4.Value = Convert.ToDecimal(acc4);
            this.num_dec4.Value = Convert.ToDecimal(dec4);
            this.num_sramp4.Value = Convert.ToDecimal(sramp4);

            this.num_unit5.Value = Convert.ToDecimal(unit5);
            this.num_velmin5.Value = Convert.ToDecimal(velMin5);
            this.num_velmax5.Value = Convert.ToDecimal(velMax5);
            this.num_acc5.Value = Convert.ToDecimal(acc5);
            this.num_dec5.Value = Convert.ToDecimal(dec5);
            this.num_sramp5.Value = Convert.ToDecimal(sramp5);

            this.num_unit6.Value = Convert.ToDecimal(unit6);
            this.num_velmin6.Value = Convert.ToDecimal(velMin6);
            this.num_velmax6.Value = Convert.ToDecimal(velMax6);
            this.num_acc6.Value = Convert.ToDecimal(acc6);
            this.num_dec6.Value = Convert.ToDecimal(dec6);
            this.num_sramp6.Value = Convert.ToDecimal(sramp6);

            this.num_unit7.Value = Convert.ToDecimal(unit7);
            this.num_velmin7.Value = Convert.ToDecimal(velMin7);
            this.num_velmax7.Value = Convert.ToDecimal(velMax7);
            this.num_acc7.Value = Convert.ToDecimal(acc7);
            this.num_dec7.Value = Convert.ToDecimal(dec7);
            this.num_sramp7.Value = Convert.ToDecimal(sramp7);
            #endregion

            #endregion
            Task.Run(new Action(() =>
            {
                CheckAutoStop();
            }));
            this.timer1.Enabled = true;
            this.MainTimer.Enabled = true;

            if (ReadDI(DI["急停"]) == 0)
            {
                AddLog(2, "急停按下");
            }
        }


        private void FrmMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
            //关闭抱闸
            BaozhaReset();
            //传递关闭窗口的句柄
            CommendMethods.bClose = true;
            Thread.Sleep(20);
            //杀死进程
            System.Diagnostics.Process.GetCurrentProcess().Kill(); // 杀掉进程



        }
        #endregion

        #region 检测停止按钮
        private void CheckAutoStop()
        {
            while (true && Simulate == false)
            {
                if (ReadDI(DI["急停"]) == 0 || IsEmgStop || CommendMethods.bClose)
                {
                    IsAutoStop = true;
                    zmotion.StopAllAxis();
                }
                if (ReadDI(DI["手动门关到位1"]) == 0 && AutoMode == true)
                {
                    IsPause = true;
                    zmotion.StopAllAxis();
                    this.Invoke(new Action(() =>
                    {
                        this.btn_Pause.Text = IsPause ? "取消暂停" : "暂停运行";
                        this.btn_Pause.BackColor = IsPause ? Color.Red : SystemColors.ControlLight;
                        this.btn_Pause.ForeColor = IsPause ? Color.White : SystemColors.ControlText;
                    }));
                }
                if (isAutoTaskRunning && AutoMode == false)
                {
                    IsAutoStop = true;
                    zmotion.StopAllAxis();
                }


            }
        }
        #endregion

        #region DI/DO绑定
        public void DigitalSignal()
        {
            DI.Add("急停", 0);
            DI.Add("自动门开到位", 1);
            DI.Add("自动门关到位", 2);
            DI.Add("手爪抱紧到位", 3);
            DI.Add("手爪打开到位", 4);
            DI.Add("磁铁上升到位", 5);
            DI.Add("磁铁下降到位", 6);
            DI.Add("翻转轴正转到位", 7);
            DI.Add("翻转轴反转到位", 8);
            DI.Add("光源上升到位", 9);
            DI.Add("光源下降到位", 10);
            DI.Add("手动门关到位1", 11);
            //DI.Add("手爪打开到位", 12);
            //DI.Add("磁铁上升到位", 13);
            //DI.Add("磁铁下降到位", 14);
            //DI.Add("翻转轴正转到位", 15);
            //DI.Add("翻转轴反转到位", 16);
            //DI.Add("光源上升到位", 17);
            //DI.Add("光源下降到位", 18);
            //DI.Add("上面相机安全位置", 19);
            //DI.Add("侧面相机安全位置", 20);
            //DI.Add("手动门关到位1", 21);
            ////DI.Add("手动门关到位2", 22);
            //DI.Add("启动", 25);

            DO.Add("自动门开", 0);
            DO.Add("自动门关", 1);
            DO.Add("手爪打开", 2);
            DO.Add("手爪抱紧", 3);
            DO.Add("磁铁上升下降", 4);
            DO.Add("光源上升下降", 5);
            DO.Add("吹气", 6);
            DO.Add("抱闸1", 7);
            DO.Add("抱闸2", 8);
            DO.Add("抱闸3", 9);
            DO.Add("上面光源开关", 10);
            DO.Add("侧面光源开关", 11);
            //DO.Add("上面光源开关", 12);
            //DO.Add("侧面光源开关", 13);
            //DO.Add("抱闸1", 64);
            //DO.Add("抱闸2", 65);
            //DO.Add("抱闸3", 66);


            //DO.Add("测试", 70);
        }


        #endregion

        #region 连接轴卡
        //连接轴卡
        private void btn_Connect_Click_1(object sender, EventArgs e)
        {
            if (this.btn_Connect.Text == "连接")
            {
                zmotion = new Zmotion(txt_IPAdress.Text.Trim());
                if (zmotion.IniCard().IsSuccess)
                {
                    this.btn_Connect.Text = "断开";
                    this.led_State.ColorList = new Color[] { Color.Green };
                    isConnectOK = true;
                    //设置脉冲当量
                    SetUnitINI();
                    //设置限位
                    SetFRLimit();
                    AddLog(0, "连接控制器成功");
                    //打开抱闸
                    Baozha();

                    //判断初始化是否成功
                    //Delay(1000);
                    float tempstatus1;
                    int m_BusNodeNum1;
                    float m_BusAxisNum1;
                    zmotion.BusIniRead(out tempstatus1, out m_BusNodeNum1, out m_BusAxisNum1);
                    if (tempstatus1 == 1 && m_BusAxisNum1 == 8)
                    {
                        //初始化成功
                        return;
                    }
                    else
                    {
                        return;
                    }

                }
                else
                {
                    isConnectOK = false;
                    AddLog(2, "连接控制器失败");
                }
            }
            else
            {
                BaozhaReset();
                if (zmotion.CloseCard().IsSuccess)
                {
                    this.btn_Connect.Text = "连接";
                    this.led_State.ColorList = new Color[] { Color.Red };
                    isConnectOK = false;
                    //如果已经打到自动模式，需切换回手动模式
                    if (AutoMode)
                    {
                        AutoMode = !AutoMode;
                        this.btn_Auto.BackColor = AutoMode ? Color.FromArgb(0, 192, 0) : SystemColors.ControlLight;
                        this.btn_Auto.ForeColor = AutoMode ? Color.White : SystemColors.ControlText;
                        this.btn_Auto.Text = AutoMode ? "自动模式" : "手动模式";
                        AddLog(0, "轴卡断开成功，模式切换为手动模式");
                        return;
                    }
                    AddLog(0, "断开控制器成功");
                }
                else
                {
                    AddLog(2, "断开控制器失败");
                }
            }
        }





        #endregion

        #region 设置参数
        //设置单轴运动参数
        private void SetParamSingle()
        {
            velMax = Convert.ToSingle(this.num_velmax.Value);
            velMin = 0;
            acc = Convert.ToSingle(this.num_acc.Value);
            dec = Convert.ToSingle(this.num_dec.Value);
            sramp = Convert.ToSingle(this.num_sramp.Value);


            zmotion.unin0 = Convert.ToInt32(this.num_unit0.Value);
            zmotion.unin1 = Convert.ToInt32(this.num_unit1.Value);
            zmotion.unin2 = Convert.ToInt32(this.num_unit2.Value);
            zmotion.unin3 = Convert.ToInt32(this.num_unit3.Value);
            zmotion.unin4 = Convert.ToInt32(this.num_unit4.Value);
            zmotion.unin5 = Convert.ToInt32(this.num_unit5.Value);
            zmotion.unin6 = Convert.ToInt32(this.num_unit6.Value);
            zmotion.unin7 = Convert.ToInt32(this.num_unit7.Value);

        }
        //设置多轴运动参数
        private void SetParamMulty()
        {
            velMax = Convert.ToSingle(this.num_mulvelmax.Value);
            velMin = 0;
            acc = Convert.ToSingle(this.num_mulacc.Value);
            dec = Convert.ToSingle(this.num_muldec.Value);
            sramp = Convert.ToSingle(this.num_mulsramp.Value);


            zmotion.unin0 = Convert.ToInt32(this.num_unit0.Value);
            zmotion.unin1 = Convert.ToInt32(this.num_unit1.Value);
            zmotion.unin2 = Convert.ToInt32(this.num_unit2.Value);
            zmotion.unin3 = Convert.ToInt32(this.num_unit3.Value);
            zmotion.unin4 = Convert.ToInt32(this.num_unit4.Value);
            zmotion.unin5 = Convert.ToInt32(this.num_unit5.Value);
            zmotion.unin6 = Convert.ToInt32(this.num_unit6.Value);
            zmotion.unin7 = Convert.ToInt32(this.num_unit7.Value);

        }
        //设置自动运动参数
        private void SetParamAuto()
        {

            velMax = 20;
            velMin = 0;
            acc = 1000;
            dec = 1000;
            sramp = 100;

            zmotion.unin0 = 46603;
            zmotion.unin1 = 46603;
            zmotion.unin2 = 46603;
            zmotion.unin3 = 46603;
            zmotion.unin4 = 46603;
            zmotion.unin5 = 46603;
            zmotion.unin6 = 46603;
            zmotion.unin7 = 46603;


        }

        private void GetParam()
        {
            unit0 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机竖轴0", "脉冲当量", "", CommendMethods.SysSetPath));
            unit1 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机横轴1", "脉冲当量", "", CommendMethods.SysSetPath));
            unit2 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机竖轴2", "脉冲当量", "", CommendMethods.SysSetPath));
            unit3 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机横轴3", "脉冲当量", "", CommendMethods.SysSetPath));
            unit4 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "脉冲当量", "", CommendMethods.SysSetPath));
            unit5 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "脉冲当量", "", CommendMethods.SysSetPath));
            unit6 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘上升轴6", "脉冲当量", "", CommendMethods.SysSetPath));
            unit7 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "脉冲当量", "", CommendMethods.SysSetPath));

            zmotion.unin0 = unit0;
            zmotion.unin1 = unit1;
            zmotion.unin2 = unit2;
            zmotion.unin3 = unit3;
            zmotion.unin4 = unit4;
            zmotion.unin5 = unit5;
            zmotion.unin6 = unit6;
            zmotion.unin7 = unit7;


            velMin0 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机竖轴0", "起始速度", "", CommendMethods.SysSetPath));
            velMax0 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机竖轴0", "运行速度", "", CommendMethods.SysSetPath));
            acc0 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机竖轴0", "加速度", "", CommendMethods.SysSetPath));
            dec0 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机竖轴0", "减速度", "", CommendMethods.SysSetPath));
            sramp0 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机竖轴0", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin1 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机横轴1", "起始速度", "", CommendMethods.SysSetPath));
            velMax1 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机横轴1", "运行速度", "", CommendMethods.SysSetPath));
            acc1 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机横轴1", "加速度", "", CommendMethods.SysSetPath));
            dec1 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机横轴1", "减速度", "", CommendMethods.SysSetPath));
            sramp1 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("上面相机横轴1", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin2 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机竖轴2", "起始速度", "", CommendMethods.SysSetPath));
            velMax2 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机竖轴2", "运行速度", "", CommendMethods.SysSetPath));
            acc2 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机竖轴2", "加速度", "", CommendMethods.SysSetPath));
            dec2 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机竖轴2", "减速度", "", CommendMethods.SysSetPath));
            sramp2 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机竖轴2", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin3 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机横轴3", "起始速度", "", CommendMethods.SysSetPath));
            velMax3 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机横轴3", "运行速度", "", CommendMethods.SysSetPath));
            acc3 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机横轴3", "加速度", "", CommendMethods.SysSetPath));
            dec3 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机横轴3", "减速度", "", CommendMethods.SysSetPath));
            sramp3 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机横轴3", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin4 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "起始速度", "", CommendMethods.SysSetPath));
            velMax4 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "运行速度", "", CommendMethods.SysSetPath));
            acc4 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "加速度", "", CommendMethods.SysSetPath));
            dec4 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "减速度", "", CommendMethods.SysSetPath));
            sramp4 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin5 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "起始速度", "", CommendMethods.SysSetPath));
            velMax5 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "运行速度", "", CommendMethods.SysSetPath));
            acc5 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "加速度", "", CommendMethods.SysSetPath));
            dec5 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "减速度", "", CommendMethods.SysSetPath));
            sramp5 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin6 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘上升轴6", "起始速度", "", CommendMethods.SysSetPath));
            velMax6 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘上升轴6", "运行速度", "", CommendMethods.SysSetPath));
            acc6 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘上升轴6", "加速度", "", CommendMethods.SysSetPath));
            dec6 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘上升轴6", "减速度", "", CommendMethods.SysSetPath));
            sramp6 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘上升轴6", "S曲线时间", "", CommendMethods.SysSetPath));

            velMin7 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "起始速度", "", CommendMethods.SysSetPath));
            velMax7 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "运行速度", "", CommendMethods.SysSetPath));
            acc7 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "加速度", "", CommendMethods.SysSetPath));
            dec7 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "减速度", "", CommendMethods.SysSetPath));
            sramp7 = (float)Convert.ToDouble(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "S曲线时间", "", CommendMethods.SysSetPath));

            //读取三菱PLC参数
            string str = IniConfigHelper.ReadIniData("PLC型号", "PLC", "", CommendMethods.PlcSetPath);
            if (str == "1")
            {
                this.rdo_sanling.Checked = true;
            }
            if (str == "2")
            {
                this.rdo_ximenzi.Checked = true;
            }
            this.txt_StartMachine.Text = IniConfigHelper.ReadIniData("PLC到设备", "设备启动地址", "", CommendMethods.PlcSetPath);
            this.txt_RobotCall.Text = IniConfigHelper.ReadIniData("PLC到设备", "Robot请求进入", "", CommendMethods.PlcSetPath);
            this.txt_RobotIn.Text = IniConfigHelper.ReadIniData("PLC到设备", "Robot已进入", "", CommendMethods.PlcSetPath);
            this.txt_Prefix.Text = IniConfigHelper.ReadIniData("PLC到设备", "型号前缀", "", CommendMethods.PlcSetPath);
            this.txt_Model.Text = IniConfigHelper.ReadIniData("PLC到设备", "型号", "", CommendMethods.PlcSetPath);
            this.txt_MachineOK.Text = IniConfigHelper.ReadIniData("设备到PLC", "设备准备OK", "", CommendMethods.PlcSetPath);
            this.txt_MachineRun.Text = IniConfigHelper.ReadIniData("设备到PLC", "设备已启动", "", CommendMethods.PlcSetPath);
            this.txt_CheckOK.Text = IniConfigHelper.ReadIniData("设备到PLC", "检测OK", "", CommendMethods.PlcSetPath);
            this.txt_CheckNG.Text = IniConfigHelper.ReadIniData("设备到PLC", "检测NG", "", CommendMethods.PlcSetPath);
            this.txt_PosOK.Text = IniConfigHelper.ReadIniData("设备到PLC", "位置度OK", "", CommendMethods.PlcSetPath);
            this.txt_PosNG.Text = IniConfigHelper.ReadIniData("设备到PLC", "位置度NG", "", CommendMethods.PlcSetPath);
            this.txt_PlcIP.Text = IniConfigHelper.ReadIniData("地址及端口", "IP地址", "", CommendMethods.PlcSetPath);
            this.txt_PlcPort.Text = IniConfigHelper.ReadIniData("地址及端口", "端口号", "", CommendMethods.PlcSetPath);
            this.txt_heart.Text = IniConfigHelper.ReadIniData("心跳地址", "心跳", "", CommendMethods.PlcSetPath);

        }
        //初始化读取保存的脉冲参数
        private void SetOnlyUnit()
        {
            //单轴运动参数
            this.num_unit0.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机竖轴0", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit1.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机横轴1", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit2.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机竖轴2", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit3.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit4.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit5.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit6.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘上升轴6", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_unit7.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "脉冲当量", "", CommendMethods.SysSetPath));

            //多轴运动参数
            this.num_mulunit0.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机竖轴0", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit1.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机横轴1", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit2.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机竖轴2", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit3.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit4.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit5.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit6.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘上升轴6", "脉冲当量", "", CommendMethods.SysSetPath));
            this.num_mulunit7.Value = Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "脉冲当量", "", CommendMethods.SysSetPath));

        }

        //基准值读取
        private void ReadBasePos()
        {

            //上面相机
            Axis0Base = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机竖轴0", "基准位置", "", CommendMethods.PosBasePath));
            Axis0Safe = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机竖轴0", "安全位置", "", CommendMethods.PosBasePath));
            Axis0limitUp = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机竖轴0", "上方极限位置", "", CommendMethods.PosBasePath));
            Axis0limitDown = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机竖轴0", "下方极限位置", "", CommendMethods.PosBasePath));
            Axis1Base = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机横轴1", "基准位置", "", CommendMethods.PosBasePath));
            Axis1Safe = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机横轴1", "安全位置", "", CommendMethods.PosBasePath));
            Axis1limitOut = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机横轴1", "远离极限位置", "", CommendMethods.PosBasePath));
            Axis1limitIn = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("上面相机横轴1", "靠近极限位置", "", CommendMethods.PosBasePath));
            //侧面相机
            Axis2Base = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机竖轴2", "基准位置", "", CommendMethods.PosBasePath));
            Axis2Safe = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机竖轴2", "安全位置", "", CommendMethods.PosBasePath));
            Axis2limitUp = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机竖轴2", "上方极限位置", "", CommendMethods.PosBasePath));
            Axis2limitDown = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机竖轴2", "下方极限位置", "", CommendMethods.PosBasePath));
            Axis3Base = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "基准位置", "", CommendMethods.PosBasePath));
            Axis3Baseqx = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "倾斜基准位置", "", CommendMethods.PosBasePath));
            Axis3Safe = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "安全位置", "", CommendMethods.PosBasePath));
            Axis3limitOut = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "远离极限位置", "", CommendMethods.PosBasePath));
            Axis3limitIn = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机横轴3", "靠近极限位置", "", CommendMethods.PosBasePath));
            Axis4Base1 = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "水平位置", "", CommendMethods.PosBasePath));
            Axis4Baseqx = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "倾斜位置", "", CommendMethods.PosBasePath));
            Axis4Safe = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("侧面相机旋转轴4", "安全位置", "", CommendMethods.PosBasePath));
            //旋转轴
            Axis5Base = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘旋转轴5", "基准位置", "", CommendMethods.PosBasePath));
            //上升轴
            Axis6Wait = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘上升轴6", "等待位置", "", CommendMethods.PosBasePath));
            Axis6GripBase = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘上升轴6", "抓取基准位置", "", CommendMethods.PosBasePath));
            Axis6Up = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘上升轴6", "上升基准位置", "", CommendMethods.PosBasePath));
            Axis6Down = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘上升轴6", "下降基准位置", "", CommendMethods.PosBasePath));
            //翻面轴
            Axis7Ppos = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "正转位置", "", CommendMethods.PosBasePath));
            Axis7Npos = (float)Convert.ToDecimal(IniConfigHelper.ReadIniData("刹车盘翻面轴7", "反转位置", "", CommendMethods.PosBasePath));
        }

        //基准值读取
        private void ReadMitsubishi()
        {

            //上面相机
            plcModel = IniConfigHelper.ReadIniData("PLC型号", "PLC", "", CommendMethods.PlcSetPath);
            startMachine = IniConfigHelper.ReadIniData("PLC到设备", "设备启动地址", "", CommendMethods.PlcSetPath);
            robotCall = IniConfigHelper.ReadIniData("PLC到设备", "Robot请求进入", "", CommendMethods.PlcSetPath);
            robotIn = IniConfigHelper.ReadIniData("PLC到设备", "Robot已进入", "", CommendMethods.PlcSetPath);
            preFix = IniConfigHelper.ReadIniData("PLC到设备", "型号前缀", "", CommendMethods.PlcSetPath);
            model = IniConfigHelper.ReadIniData("PLC到设备", "型号", "", CommendMethods.PlcSetPath);
            machineOK = IniConfigHelper.ReadIniData("设备到PLC", "设备准备OK", "", CommendMethods.PlcSetPath);
            machineRun = IniConfigHelper.ReadIniData("设备到PLC", "设备已启动", "", CommendMethods.PlcSetPath);
            checkOK = IniConfigHelper.ReadIniData("设备到PLC", "检测OK", "", CommendMethods.PlcSetPath);
            checkNG = IniConfigHelper.ReadIniData("设备到PLC", "检测NG", "", CommendMethods.PlcSetPath);
            posOK = IniConfigHelper.ReadIniData("设备到PLC", "位置度OK", "", CommendMethods.PlcSetPath);
            posNG = IniConfigHelper.ReadIniData("设备到PLC", "位置度NG", "", CommendMethods.PlcSetPath);
            IPMitsu = IniConfigHelper.ReadIniData("地址及端口", "IP地址", "", CommendMethods.PlcSetPath);
            PortMitsu = IniConfigHelper.ReadIniData("地址及端口", "端口号", "", CommendMethods.PlcSetPath);
            Heart = IniConfigHelper.ReadIniData("心跳地址", "心跳", "", CommendMethods.PlcSetPath);
        }


        #endregion

        #region 使能操作
        private void btn_Enable(object sender, EventArgs e)
        {
            if (sender is Button btn)
            {
                if (btn.Tag != null && btn.Tag.ToString().Length > 0)
                {
                    //获取轴号
                    short axis = Convert.ToInt16(btn.Tag.ToString());
                    if (btn.Text == "使能")
                    {
                        var result = zmotion.ServeON(axis);
                        if (result.IsSuccess)
                        {
                            btn.BackColor = Color.Green;
                            btn.ForeColor = Color.White;
                            btn.Text = "使能断开";
                        }
                        else
                        {
                            AddLog(2, "使能打开失败");
                        }
                        return;
                    }

                    if (btn.Text == "使能断开")
                    {
                        var result = zmotion.ServeOFF(axis);
                        if (result.IsSuccess)
                        {
                            btn.BackColor = SystemColors.Control;
                            btn.ForeColor = Color.Black;
                            btn.Text = "使能";
                        }
                        else
                        {
                            AddLog(2, "使能关闭失败");
                        }
                        return;
                    }


                }

            }

        }

        #endregion

        #region 单轴运动
        private void btn_JOG_MouseDown(object sender, MouseEventArgs e)
        {
            if (sender is Button btn)
            {
                if (btn.Tag != null && btn.Tag.ToString().Length > 0)
                {
                    string[] values = btn.Tag.ToString().Split(';');
                    if (values.Length == 2)
                    {
                        //获取轴号
                        short axis = Convert.ToInt16(values[0]);
                        //获取方向
                        bool dir = values[1] == "1";
                        SetParamSingle();
                        var result = zmotion.VMove(axis, velMax, dir, velMin, acc, dec, sramp);
                        if (result.IsSuccess == false)
                        {
                            AddLog(1, "点动失败");
                        }
                    }
                }

            }
        }

        private void btn_JOG_MouseUp(object sender, MouseEventArgs e)
        {
            if (sender is Button btn)
            {
                if (btn.Tag != null && btn.Tag.ToString().Length > 0)
                {
                    string[] values = btn.Tag.ToString().Split(';');
                    if (values.Length == 2)
                    {
                        //获取轴号
                        short axis = Convert.ToInt16(values[0]);
                        //获取反向
                        bool dir = values[1] == "1";
                        SetParamSingle();
                        var result = zmotion.StopAxis(axis);
                        if (result.IsSuccess == false)
                        {
                            AddLog(2, "点动停止失败");
                        }
                    }
                }

            }
        }

        private void btn_MoveAbs_Click(object sender, EventArgs e)
        {
            SetParamSingle();
            if (Axis < 0)
            {
                return;
            }
            //判断绝对位置是否超出限位
            OutLimit = false;
            JudgeAbsPos(Axis, Distance);
            if (OutLimit == true)
            {
                //超出限位
                return;
            }
            var result = zmotion.MoveABS(Axis, velMax, Distance, velMin, acc, dec, sramp);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("绝对运动失败" + result.ErrorMsg, "绝对运动");
            }
        }

        private void btn_MoveRelative_Click(object sender, EventArgs e)
        {
            SetParamSingle();
            if (Axis < 0)
            {
                return;
            }
            //判断相对位置是否超出限位
            OutLimit = false;
            JudgeMultyPos(Axis, Distance);
            if (OutLimit == true)
            {
                //超出限位
                return;
            }
            var result = zmotion.MoveRelative(Axis, velMax, Distance, velMin, acc, dec, sramp);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("相对运动失败" + result.ErrorMsg, "相对运动");
            }
        }

        private void btn_MoveStop_Click(object sender, EventArgs e)
        {
            zmotion.StopAllAxis();
            //var result = zmotion.StopAxis(Axis);
            //if (result.IsSuccess == false)
            //{
            //    MessageBox.Show("停止轴运动失败" + result.ErrorMsg, "停止");
            //}
        }

        private void btn_ZeroAxis_Click(object sender, EventArgs e)
        {
            if (Axis < 0)
            {
                return;
            }
            var result = zmotion.ZeroPos(Axis);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("轴清零失败" + result.ErrorMsg, "轴清零");
            }
        }




        #endregion

        #region 实时监控
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (zmotion.InitedOK)
            {
                //获取当前速度
                this.txt_speed0.Text = zmotion.GetVel(upAxis0).ToString("f1");
                this.txt_speed1.Text = zmotion.GetVel(upAxis1).ToString("f1");
                this.txt_speed2.Text = zmotion.GetVel(sideAxis1).ToString("f1");
                this.txt_speed3.Text = zmotion.GetVel(sideAxis2).ToString("f1");
                this.txt_speed4.Text = zmotion.GetVel(sideAxis3).ToString("f1");
                this.txt_speed5.Text = zmotion.GetVel(rotateAxis).ToString("f1");
                this.txt_speed6.Text = zmotion.GetVel(updownAxis).ToString("f1");
                this.txt_speed7.Text = zmotion.GetVel(flipAxis).ToString("f1");

                //获取当前位置
                this.txt_pos0.Text = zmotion.GetMos(upAxis0).ToString("f1");
                this.txt_pos1.Text = zmotion.GetMos(upAxis1).ToString("f1");
                this.txt_pos2.Text = zmotion.GetMos(sideAxis1).ToString("f1");
                this.txt_pos3.Text = zmotion.GetMos(sideAxis2).ToString("f1");
                this.txt_pos4.Text = zmotion.GetMos(sideAxis3).ToString("f1");
                this.txt_pos5.Text = zmotion.GetMos(rotateAxis).ToString("f1");
                this.txt_pos6.Text = zmotion.GetMos(updownAxis).ToString("f1");
                this.txt_pos7.Text = zmotion.GetMos(flipAxis).ToString("f1");


                //读取轴状态
                //调试界面显示
                this.txt_axisStatus0.Text = zmotion.ReadAxisStatus(upAxis0).ToString("f1");
                this.txt_axisStatus1.Text = zmotion.ReadAxisStatus(upAxis1).ToString("f1");
                this.txt_axisStatus2.Text = zmotion.ReadAxisStatus(sideAxis1).ToString("f1");
                this.txt_axisStatus3.Text = zmotion.ReadAxisStatus(sideAxis2).ToString("f1");
                this.txt_axisStatus4.Text = zmotion.ReadAxisStatus(sideAxis3).ToString("f1");
                this.txt_axisStatus5.Text = zmotion.ReadAxisStatus(rotateAxis).ToString("f1");
                this.txt_axisStatus6.Text = zmotion.ReadAxisStatus(updownAxis).ToString("f1");
                this.txt_axisStatus7.Text = zmotion.ReadAxisStatus(flipAxis).ToString("f1");


                //获取运动状态
                bool state0 = zmotion.IsMoving(upAxis0);
                this.lbl_AState0.BackColor = state0 ? Color.LimeGreen : Color.Red;
                bool state1 = zmotion.IsMoving(upAxis1);
                this.lbl_AState1.BackColor = state1 ? Color.LimeGreen : Color.Red;
                bool state2 = zmotion.IsMoving(sideAxis1);
                this.lbl_AState2.BackColor = state2 ? Color.LimeGreen : Color.Red;
                bool state3 = zmotion.IsMoving(sideAxis2);
                this.lbl_AState3.BackColor = state3 ? Color.LimeGreen : Color.Red;
                bool state4 = zmotion.IsMoving(sideAxis3);
                this.lbl_AState4.BackColor = state4 ? Color.LimeGreen : Color.Red;
                bool state5 = zmotion.IsMoving(rotateAxis);
                this.lbl_AState5.BackColor = state5 ? Color.LimeGreen : Color.Red;
                bool state6 = zmotion.IsMoving(updownAxis);
                this.lbl_AState6.BackColor = state6 ? Color.LimeGreen : Color.Red;
                bool state7 = zmotion.IsMoving(flipAxis);
                this.lbl_AState7.BackColor = state7 ? Color.LimeGreen : Color.Red;


            }







        }

        private void MainTimer_Tick(object sender, EventArgs e)
        {
            //读取轴状态
            //主界面显示  txt_0axisStatus
            this.txt_0axisStatus.Text = zmotion.ReadAxisStatus(upAxis0).ToString("f1");
            this.txt_1axisStatus.Text = zmotion.ReadAxisStatus(upAxis1).ToString("f1");
            this.txt_2axisStatus.Text = zmotion.ReadAxisStatus(sideAxis1).ToString("f1");
            this.txt_3axisStatus.Text = zmotion.ReadAxisStatus(sideAxis2).ToString("f1");
            this.txt_4axisStatus.Text = zmotion.ReadAxisStatus(sideAxis3).ToString("f1");
            this.txt_5axisStatus.Text = zmotion.ReadAxisStatus(rotateAxis).ToString("f1");
            this.txt_6axisStatus.Text = zmotion.ReadAxisStatus(updownAxis).ToString("f1");
            this.txt_7axisStatus.Text = zmotion.ReadAxisStatus(flipAxis).ToString("f1");

            //初始化
            float tempstatus;
            int m_BusNodeNum;
            float m_BusAxisNum;
            zmotion.BusIniRead(out tempstatus, out m_BusNodeNum, out m_BusAxisNum);

            if (tempstatus != -1)     //初始化完成刷新状态	
            {
                C_Bus_Status.Text = (tempstatus == 1) ? "初始化成功" : "初始化失败";
                C_NodeNum.Text = m_BusNodeNum.ToString();
                C_AxisNum.Text = m_BusAxisNum.ToString();
                AxisNum = m_BusAxisNum;
            }

            #region 读写信号状态
            this.lbl_EMG.BackColor = ReadDI(DI["急停"]) == 0 ? Color.Green : Color.Red;
            this.lbl_AutoDoorOpen.BackColor = ReadDI(DI["自动门开到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_AutoDoorClose.BackColor = ReadDI(DI["自动门关到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_crampOpen.BackColor = ReadDI(DI["手爪打开到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_crampClose.BackColor = ReadDI(DI["手爪抱紧到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_MagUp.BackColor = ReadDI(DI["磁铁上升到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_MagDown.BackColor = ReadDI(DI["磁铁下降到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_flipP.BackColor = ReadDI(DI["翻转轴正转到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_flipN.BackColor = ReadDI(DI["翻转轴反转到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_LightUp.BackColor = ReadDI(DI["光源上升到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_LightDown.BackColor = ReadDI(DI["光源下降到位"]) == 1 ? Color.Green : Color.Red;
            this.lbl_ManuDoorClose.BackColor = ReadDI(DI["手动门关到位1"]) == 1 ? Color.Green : Color.Red;
            #endregion

            #region 原点信号
            //this.lbl_Zero0.BackColor = ReadDI(DI["1轴零点"]) == 1 ? Color.Green : Color.Red;
            //this.lbl_Zero1.BackColor = ReadDI(DI["2轴零点"]) == 1 ? Color.Green : Color.Red;
            //this.lbl_Zero2.BackColor = ReadDI(DI["3轴零点"]) == 1 ? Color.Green : Color.Red;
            //this.lbl_Zero3.BackColor = ReadDI(DI["4轴零点"]) == 1 ? Color.Green : Color.Red;
            ////this.lbl_Zero4.BackColor = ReadDI(DI["5轴零点"]) == 1 ? Color.Green : Color.Red;
            ////this.lbl_Zero5.BackColor = ReadDI(DI["6轴零点"]) == 1 ? Color.Green : Color.Red;
            //this.lbl_Zero6.BackColor = ReadDI(DI["7轴零点"]) == 1 ? Color.Green : Color.Red;
            ////this.lbl_Zero7.BackColor = ReadDI(DI["8轴零点"]) == 1 ? Color.Green : Color.Red;

            #endregion


            #region 读取使能状态
            ReadServo();

            #endregion




        }


        #endregion

        #region SDO读取
        //读取0X6041
        public string SDOREAD6041(uint slot, uint node, uint index, uint subindex, uint type)
        {
            int value = zmotion.SDOREAD(slot, node, index, subindex, type);
            string valueString = Convert.ToString(value, 2);
            int num = 16 - valueString.Length;
            if (num != 0)
            {
                for (int i = 0; i < num; i++)
                {
                    valueString = "0" + valueString;
                }
            }
            //返回bit7，如果驱动器有报警的话，返回1，否则返回0
            return valueString.Substring(8, 1);
        }
        //读取0X2001
        public string SDOREAD2001(uint slot, uint node, uint index, uint subindex, uint type)
        {
            int value = zmotion.SDOREAD(slot, node, index, subindex, type);
            string valueString = Convert.ToString(value, 16);
            int num = 3 - valueString.Length;
            if (num != 0)
            {
                valueString = "0" + valueString;
            }
            valueString = "AL" + valueString;
            //返回驱动器当前报警代码
            return valueString;
        }

        #endregion

        #region 报警复位
        private void btn_AlarmReset_Click(object sender, EventArgs e)
        {
            zmotion.AlarmReset(upAxis0);
            zmotion.AlarmReset(upAxis1);
            zmotion.AlarmReset(sideAxis1);
            zmotion.AlarmReset(sideAxis2);
            zmotion.AlarmReset(sideAxis3);
            zmotion.AlarmReset(rotateAxis);
            zmotion.AlarmReset(updownAxis);
            zmotion.AlarmReset(flipAxis);

        }

        #endregion

        #region 多轴运动通用方法
        /// <summary>
        /// 上面相机移动
        /// </summary>
        /// <param name="dis0"></param>
        /// <param name="dis1"></param>
        /// <param name="velMax"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="status">1表示 等待停止</param>
        /// <returns>返回1，运动成功，返回-1运动失败</returns>
        private int UpCameraMove01(float dis0, float dis1, float velMax, float acc, float dec, float status)
        {
            if (ReadDI(DI["急停"]) == 1)
            {
                float pos = (zmotion.GetMos(1));
                //if (pos > Axis1Safe + 2)
                //{
                //    MessageBox.Show("上面相机横轴不在安全位置，请确认");
                //    return -22;
                //}
                int[] axis;
                float[] distance;
                axis = new int[] { upAxis0, upAxis1 };
                distance = new float[] { dis0, dis1 };
                OutLimit = false;
                JudgeAbsPos(0, dis0);
                JudgeAbsPos(1, dis1);
                if (OutLimit == true)
                {
                    AddLog(1, "0/1轴超范围");
                    return -12;
                }
                var result = zmotion.MoveLineABS(axis, distance, velMax, acc, dec);
                Delay(100);
                if (status == 1)
                {
                    JudgeAxisStop(0, 1);
                }
                if (result.IsSuccess == true)
                {
                    return 1;
                }
                else
                {
                    //运动失败
                    AddLog(1, "01直线插补运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                    return -10;
                }
            }
            else
            {
                return -99;
            }

        }
        /// <summary>
        /// 侧面相机移动
        /// </summary>
        /// <param name="dis0"></param>
        /// <param name="dis1"></param>
        /// <param name="dis2"></param>
        /// <param name="velMax"></param>
        /// <param name="acc"></param>
        /// <param name="dec"></param>
        /// <param name="status">运动类型 1是精确到达</param>
        /// <returns>返回-1 说明运动失败,返回 1运动成功</returns>
        private int SideCameraMove234(float dis0, float dis1, float dis2, float velMax, float acc, float dec, float status)
        {
            if (ReadDI(DI["急停"]) == 1)
            {
                int[] axis;
                float[] distance;
                var result = OperationResult.CreatFailResult();
                axis = new int[] { sideAxis1, sideAxis2, sideAxis3 };
                distance = new float[] { dis0, dis1, dis2 };
                OutLimit = false;
                JudgeAbsPos(2, dis0);
                JudgeAbsPos(3, dis1);
                JudgeAbsPos(4, dis2);
                if (OutLimit == true)
                {
                    AddLog(1, "2/3/4轴超范围");
                    return -345;
                }
                result = zmotion.MoveLineABS(axis, distance, velMax, acc, dec);
                Delay(100);
                if (status == 1)
                {
                    JudgeAxisStop(2, 3, 4);
                }
                if (result.IsSuccess == true)
                {
                    return 1;
                }
                else
                {
                    //运动失败
                    AddLog(1, "234直线插补运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                    return -10;
                }


            }
            else
            {
                return -99;
            }

        }
        /// <summary>
        /// 5轴绝对运动
        /// </summary>
        /// <param name="dis0"></param>
        private void ABSRotateAxis5(float dis0, float velMax, float velMin, float acc, float dec, float sramp)
        {
            Task task1 = Task.Run(() =>
            {
                var result = zmotion.MoveABS(rotateAxis, velMax, dis0, velMin, acc, dec, sramp);
                if (result.IsSuccess == false)
                {
                    AddLog(1, "旋转轴绝对运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                }
            });
            task1.Wait();
            Delay(50);
        }
        /// <summary>
        /// 5轴相对运动
        /// </summary>
        /// <param name="dis0"></param>
        private void MulRotateAxis5(float dis0, float velMax, float velMin, float acc, float dec, float sramp)
        {
            Task task1 = Task.Run(() =>
            {
                var result = zmotion.MoveRelative(rotateAxis, velMax, dis0, velMin, acc, dec, sramp);
                if (result.IsSuccess == false)
                {
                    AddLog(1, "旋转轴相对运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                }
            });
            task1.Wait();
            Delay(50);
        }
        /// <summary>
        /// 6轴运动
        /// </summary>
        /// <param name="dis0"></param>
        private void UpdownAxis6(float dis0, float velMax, float velMin, float acc, float dec, float sramp)
        {
            Task task1 = Task.Run(() =>
            {
                var result = zmotion.MoveABS(updownAxis, velMax, dis0, velMin, acc, dec, sramp);
                if (result.IsSuccess == false)
                {
                    AddLog(1, "升降轴绝对运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                }
            });
            task1.Wait();
            Delay(50);
        }
        /// <summary>
        /// 7轴运动
        /// </summary>
        /// <param name="dis0"></param>
        private void FlipAxis7(float dis0, float velMax, float velMin, float acc, float dec, float sramp)
        {
            Task task1 = Task.Run(() =>
            {
                var result = zmotion.MoveABS(flipAxis, velMax, dis0, velMin, acc, dec, sramp);
                if (result.IsSuccess == false)
                {
                    AddLog(1, "翻面轴绝对运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                }
            });
            task1.Wait();
            Delay(50);
        }
        /// <summary>
        /// 等待单轴停止
        /// </summary>
        /// <param name="a"></param>
        private void JudgeAxisStop(short a)
        {
            var result = zmotion.WaitStop(a);
            Delay(50);
        }
        /// <summary>
        /// 等待两轴轴停止
        /// </summary>
        /// <param name="a"></param>
        private void JudgeAxisStop(short a, short b)
        {
            var result = zmotion.WaitStop(a, b);
            Delay(100);
        }
        /// <summary>
        /// 等待三轴停止
        /// </summary>
        /// <param name="a"></param>
        private void JudgeAxisStop(short a, short b, short c)
        {
            var result = zmotion.WaitStop(a, b, c);
            Delay(50);
        }

        #endregion

        #region 输入/输出通用方法
        //读取使能状态
        public void ReadServo()
        {
            if (zmotion.InitedOK)
            {
                //1轴使能
                var result = zmotion.ReadServo(0);
                if (result.IsSuccess)
                {

                    btn_Servo0.BackColor = Color.Green;
                    btn_Servo0.ForeColor = Color.White;
                    btn_Servo0.Text = "使能断开";
                    if (zmotion.GetDO(DO["抱闸1"]) == 0)
                    {
                        //抱闸松开
                        var resultbaozha = zmotion.SetDO(DO["抱闸1"], 1);
                    }
                }
                else
                {
                    btn_Servo0.BackColor = SystemColors.Control;
                    btn_Servo0.ForeColor = Color.Black;
                    btn_Servo0.Text = "使能";
                    if (zmotion.GetDO(DO["抱闸1"]) == 1)
                    {
                        //抱闸抱紧
                        var resultbaozha = zmotion.SetDO(DO["抱闸1"], 0);
                    }
                }
                //2轴使能
                var result1 = zmotion.ReadServo(1);
                if (result1.IsSuccess)
                {

                    btn_Servo1.BackColor = Color.Green;
                    btn_Servo1.ForeColor = Color.White;
                    btn_Servo1.Text = "使能断开";
                }
                else
                {
                    btn_Servo1.BackColor = SystemColors.Control;
                    btn_Servo1.ForeColor = Color.Black;
                    btn_Servo1.Text = "使能";
                }
                //3轴使能
                var result2 = zmotion.ReadServo(2);
                if (result2.IsSuccess)
                {

                    btn_Servo2.BackColor = Color.Green;
                    btn_Servo2.ForeColor = Color.White;
                    btn_Servo2.Text = "使能断开";
                    if (zmotion.GetDO(DO["抱闸2"]) == 0)
                    {
                        var resultbaozha = zmotion.SetDO(DO["抱闸2"], 1);
                    }
                }
                else
                {
                    btn_Servo2.BackColor = SystemColors.Control;
                    btn_Servo2.ForeColor = Color.Black;
                    btn_Servo2.Text = "使能";
                    if (zmotion.GetDO(DO["抱闸2"]) == 1)
                    {
                        var resultbaozha = zmotion.SetDO(DO["抱闸2"], 0);
                    }
                }
                //4轴使能
                var result3 = zmotion.ReadServo(3);
                if (result3.IsSuccess)
                {

                    btn_Servo3.BackColor = Color.Green;
                    btn_Servo3.ForeColor = Color.White;
                    btn_Servo3.Text = "使能断开";
                }
                else
                {
                    btn_Servo3.BackColor = SystemColors.Control;
                    btn_Servo3.ForeColor = Color.Black;
                    btn_Servo3.Text = "使能";
                }
                //5轴使能
                var result4 = zmotion.ReadServo(4);
                if (result4.IsSuccess)
                {

                    btn_Servo4.BackColor = Color.Green;
                    btn_Servo4.ForeColor = Color.White;
                    btn_Servo4.Text = "使能断开";
                }
                else
                {
                    btn_Servo4.BackColor = SystemColors.Control;
                    btn_Servo4.ForeColor = Color.Black;
                    btn_Servo4.Text = "使能";
                }
                //6轴使能
                var result5 = zmotion.ReadServo(5);
                if (result5.IsSuccess)
                {

                    btn_Servo5.BackColor = Color.Green;
                    btn_Servo5.ForeColor = Color.White;
                    btn_Servo5.Text = "使能断开";
                }
                else
                {
                    btn_Servo5.BackColor = SystemColors.Control;
                    btn_Servo5.ForeColor = Color.Black;
                    btn_Servo5.Text = "使能";
                }
                //7轴使能
                var result6 = zmotion.ReadServo(6);
                if (result6.IsSuccess)
                {

                    btn_Servo6.BackColor = Color.Green;
                    btn_Servo6.ForeColor = Color.White;
                    btn_Servo6.Text = "使能断开";
                    if (zmotion.GetDO(DO["抱闸3"]) == 0)
                    {
                        var resultbaozha = zmotion.SetDO(DO["抱闸3"], 1);
                    }

                }
                else
                {
                    btn_Servo6.BackColor = SystemColors.Control;
                    btn_Servo6.ForeColor = Color.Black;
                    btn_Servo6.Text = "使能";
                    if (zmotion.GetDO(DO["抱闸3"]) == 1)
                    {
                        var resultbaozha = zmotion.SetDO(DO["抱闸3"], 0);
                    }
                }
                //8轴使能
                var result7 = zmotion.ReadServo(7);
                if (result7.IsSuccess)
                {

                    btn_Servo7.BackColor = Color.Green;
                    btn_Servo7.ForeColor = Color.White;
                    btn_Servo7.Text = "使能断开";
                }
                else
                {
                    btn_Servo7.BackColor = SystemColors.Control;
                    btn_Servo7.ForeColor = Color.Black;
                    btn_Servo7.Text = "使能";
                }

            }
        }

        //读取DI
        public uint ReadDI(int num)
        {
            uint result = zmotion.GetDI(num);
            return result;
        }

        //自动门开
        public void AutoDoorOpen()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["自动门关"], 0);
            var result2 = zmotion.SetDO(DO["自动门开"], 1);
        }
        //自动门关
        public void AutoDoorClose()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["自动门开"], 0);
            var result2 = zmotion.SetDO(DO["自动门关"], 1);
        }
        //手爪打开
        public void CrampOpen()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["手爪抱紧"], 0);
            Delay(200);
            var result2 = zmotion.SetDO(DO["手爪打开"], 1);
        }
        //手爪抱紧
        public void CrampClose()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["手爪打开"], 0);
            Delay(200);
            var result2 = zmotion.SetDO(DO["手爪抱紧"], 1);
        }
        //电磁铁上升
        public void MagUp()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result2 = zmotion.SetDO(DO["磁铁上升下降"], 1);
        }
        //电磁铁下降
        public void MagDown()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["磁铁上升下降"], 0);
        }
        //光源上升
        public int LightUp()
        {
            if (ReadDI(DI["急停"]) == 0) return -99;
            //判断一下侧面相机横轴的位置
            float pos3 = (float)Convert.ToDouble(zmotion.GetPos(3).ToString("f1"));
            float pos4 = (float)Convert.ToDouble(zmotion.GetPos(4).ToString("f1"));
            if (pos3 < Axis4Safe)
            {
                AddLog(1, "侧面相机干涉，请注意");
                return -23;

            }
            var result2 = zmotion.SetDO(DO["光源上升下降"], 1);
            return 1;
        }
        //光源下降
        public void LightDown()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["光源上升下降"], 0);
        }
        //上面光源打开
        public void UpLightOpen()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["上面光源开关"], 1);
        }
        //上面光源关闭
        public void UpLightClose()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["上面光源开关"], 0);
        }
        //侧面光源打开
        public void SideLightOpen()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["侧面光源开关"], 1);
        }
        //侧面光源关闭
        public void SideLightClose()
        {
            if (ReadDI(DI["急停"]) == 0) return;
            var result1 = zmotion.SetDO(DO["侧面光源开关"], 0);
        }
        //吹气
        public void Blow(int time)
        {
            if (ReadDI(DI["急停"]) == 0) return;
            Task.Run(() =>
            {
                var result1 = zmotion.SetDO(DO["吹气"], 1);
                Delay(time);
                result1 = zmotion.SetDO(DO["吹气"], 0);
            });

        }

        //三色灯红
        public void Three_Color_RED()
        {
            //if (ReadDI(DI["急停"]) == 0) return;
            //var result1 = zmotion.SetDO(DO["三色灯红"], 1);
            //result1 = zmotion.SetDO(DO["三色灯绿"], 0);
            //result1 = zmotion.SetDO(DO["三色灯黄"], 0);
        }
        //三色灯绿
        public void Three_Color_Green()
        {
            //if (ReadDI(DI["急停"]) == 0) return;
            //var result1 = zmotion.SetDO(DO["三色灯红"], 0);
            //result1 = zmotion.SetDO(DO["三色灯绿"], 1);
            //result1 = zmotion.SetDO(DO["三色灯黄"], 0);
        }
        //三色灯黄
        public void Three_Color_Yellow()
        {
            //if (ReadDI(DI["急停"]) == 0) return;
            //var result1 = zmotion.SetDO(DO["三色灯红"], 0);
            //result1 = zmotion.SetDO(DO["三色灯绿"], 0);
            //result1 = zmotion.SetDO(DO["三色灯黄"], 1);
        }
        //测试扩展板卡
        public void TestKuozhan()
        {
            var result = zmotion.SetDO(DO["测试"], 0);
            result = zmotion.SetDO(DO["抱闸1"], 1);
            result = zmotion.SetDO(DO["抱闸2"], 1);
            result = zmotion.SetDO(DO["抱闸3"], 1);

        }

        public void Delay(int T)
        {
            Thread.Sleep(T);
        }

        //等待信号到位
        public int WaitDigital(string str)
        {
            time.Enabled = true;
            WaitOutTime = false;
            uint a;
            do
            {
                a = ReadDI(DI[str]);
                Delay(100);
            } while (a != 1 && !WaitOutTime);
            if (WaitOutTime)
            {
                time.Enabled = false;
                WaitOutTime = false;
                switch (DI[str])
                {
                    case 1:
                        AddLog(1, str + "没有");
                        return -31;
                    case 2:
                        AddLog(1, str + "没有");
                        return -32;
                    case 3:
                        AddLog(1, str + "没有");
                        return -33;
                    case 4:
                        AddLog(1, str + "没有");
                        return -34;
                    case 5:
                        AddLog(1, str + "没有");
                        return -35;
                    case 6:
                        AddLog(1, str + "没有");
                        return -36;
                    case 15:
                        AddLog(1, str + "没有");
                        return -37;
                    case 7:
                        AddLog(1, str + "没有");
                        return -38;
                    case 8:
                        AddLog(1, str + "没有");
                        return -39;
                    case 9:
                        AddLog(1, str + "没有");
                        return -40;
                    case 10:
                        AddLog(1, str + "没有");
                        return -41;
                    case 11:
                        AddLog(1, str + "没有");
                        return -42;
                    default:
                        break;
                }
            }
            time.Enabled = false;
            WaitOutTime = false;
            return 1;
        }

        //抱闸不起作用，给24V不起作用
        public void Baozha()
        {
            var result1 = zmotion.SetDO(DO["抱闸1"], 1);
            var result2 = zmotion.SetDO(DO["抱闸2"], 1);
            var result3 = zmotion.SetDO(DO["抱闸3"], 1);
            if (result1.IsSuccess == false)
            {
                AddLog(2, "1轴抱闸未松开");
            }
            if (result2.IsSuccess == false)
            {
                AddLog(2, "3轴抱闸未松开");
            }
            if (result3.IsSuccess == false)
            {
                AddLog(2, "7轴抱闸未松开");
            }

        }

        //抱闸起作用，断开24V
        public void BaozhaReset()
        {
            var result = zmotion.SetDO(DO["抱闸1"], 0);
            result = zmotion.SetDO(DO["抱闸2"], 0);
            result = zmotion.SetDO(DO["抱闸3"], 0);
        }



        #endregion

        #region 设置8轴正负限位
        public void SetFRLimit()
        {
            #region //设置1轴正负限位上面相机竖轴
            var result = zmotion.SetFsLimit(0, 50000000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "1轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(0, -500000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "1轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置2轴正负限位上面相机横轴
            result = zmotion.SetFsLimit(1, 999999999);
            if (result.IsSuccess == false)
            {
                AddLog(1, "2轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(1, -999999999);
            if (result.IsSuccess == false)
            {
                AddLog(1, "2轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置3轴正负限位侧面相机竖轴
            result = zmotion.SetFsLimit(2, 50000000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "3轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(2, -500000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "3轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置4轴正负限位侧面相机横轴
            result = zmotion.SetFsLimit(3, 50000000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "4轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(3, -500000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "4轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置5轴正负限位侧面相机旋转轴
            result = zmotion.SetFsLimit(4, 50000000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "5轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(4, -500000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "5轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置6轴正负限位刹车盘旋转轴
            result = zmotion.SetFsLimit(5, 7200);
            if (result.IsSuccess == false)
            {
                AddLog(1, "6轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(5, -7200);
            if (result.IsSuccess == false)
            {
                AddLog(1, "6轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置7轴正负限位刹车盘上升轴
            result = zmotion.SetFsLimit(6, 50000000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "7轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(6, -500000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "7轴负限位设置失败");
                return;
            }
            #endregion

            #region //设置8轴正负限位刹车盘翻面轴
            result = zmotion.SetFsLimit(7, 50000000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "8轴正限位设置失败");
                return;
            }
            result = zmotion.SetRsLimit(7, -500000);
            if (result.IsSuccess == false)
            {
                AddLog(1, "8轴负限位设置失败");
                return;
            }
            #endregion
        }

        #endregion

        #region //初始化脉冲当量
        public void SetUnitINI()
        {
            var result = zmotion.SetUintsIni(0, unit0);
            result = zmotion.SetUintsIni(1, unit1);
            result = zmotion.SetUintsIni(2, unit2);
            result = zmotion.SetUintsIni(3, unit3);
            result = zmotion.SetUintsIni(4, unit4);
            result = zmotion.SetUintsIni(5, unit5);
            result = zmotion.SetUintsIni(6, unit6);
            result = zmotion.SetUintsIni(7, unit7);
        }



        #endregion

        #region //运行前判断轴是否超出限位
        //绝对位置判断
        public void JudgeAbsPos(short axis, float pos)
        {
            switch (axis)
            {
                case 0:
                    if (pos > Axis0limitUp || pos < Axis0limitDown)
                    {
                        OutLimit = true;
                        MessageBox.Show("1轴超出限位，请确认");
                        return;
                    }
                    break;
                case 1:
                    if (pos > Axis1limitOut || pos < Axis1limitIn)
                    {
                        OutLimit = true;
                        MessageBox.Show("2轴超出限位，请确认");
                        return;
                    }
                    break;
                case 2:
                    if (pos > Axis2limitUp || pos < Axis2limitDown)
                    {
                        OutLimit = true;
                        MessageBox.Show("3轴超出限位，请确认");
                        return;
                    }
                    break;
                case 3:
                    if (pos > Axis3limitIn || pos < Axis3limitOut)
                    {
                        OutLimit = true;
                        MessageBox.Show("4轴超出限位，请确认");
                        return;
                    }
                    break;
                case 4:
                    if (pos > 15 || pos < -60)
                    {
                        OutLimit = true;
                        MessageBox.Show("5轴超出限位，请确认");
                        return;
                    }
                    break;
                case 5:
                    if (pos > 1080 || pos < -1080)
                    {
                        OutLimit = true;
                        MessageBox.Show("6轴超出限位，请确认");
                        return;
                    }
                    break;
                case 6:
                    if (pos > 2 || pos < Axis6Up - 10)
                    {
                        OutLimit = true;
                        MessageBox.Show("7轴超出限位，请确认");
                        return;
                    }
                    break;
                case 7:
                    if (pos > 190 || pos < -5)
                    {
                        OutLimit = true;
                        MessageBox.Show("8轴超出限位，请确认");
                        return;
                    }
                    break;


                default:
                    break;
            }

        }
        //相对位置判断
        public void JudgeMultyPos(short axis, float pos)
        {
            pos = pos + (zmotion.GetMos(axis));
            switch (axis)
            {
                case 0:
                    if (pos > Axis0limitUp || pos < Axis0limitDown)
                    {
                        OutLimit = true;
                        MessageBox.Show("1轴超出限位，请确认");
                        return;
                    }
                    break;
                case 1:
                    if (pos > Axis1limitOut || pos < Axis1limitIn)
                    {
                        OutLimit = true;
                        MessageBox.Show("2轴超出限位，请确认");
                        return;
                    }
                    break;
                case 2:
                    if (pos > Axis2limitUp || pos < Axis2limitDown)
                    {
                        OutLimit = true;
                        MessageBox.Show("3轴超出限位，请确认");
                        return;
                    }
                    break;
                case 3:
                    if (pos > Axis3limitIn || pos < Axis3limitOut)
                    {
                        OutLimit = true;
                        MessageBox.Show("4轴超出限位，请确认");
                        return;
                    }
                    break;
                case 4:
                    if (pos > 15 || pos < -60)
                    {
                        OutLimit = true;
                        MessageBox.Show("5轴超出限位，请确认");
                        return;
                    }
                    break;
                case 5:
                    if (pos > 1080 || pos < -1080)
                    {
                        OutLimit = true;
                        MessageBox.Show("6轴超出限位，请确认");
                        return;
                    }
                    break;
                case 6:
                    if (pos > 2 || pos < Axis6Up - 10)
                    {
                        OutLimit = true;
                        MessageBox.Show("7轴超出限位，请确认");
                        return;
                    }
                    break;
                case 7:
                    if (pos > 190 || pos < -5)
                    {
                        OutLimit = true;
                        MessageBox.Show("8轴超出限位，请确认");
                        return;
                    }
                    break;


                default:
                    break;
            }

        }


        #endregion

        #region  多轴运动
        private void btn_mulStartXD_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            int resAbs = 0;
            if (ReadDI(DI["急停"]) == 0) return;
            resAbs = UpCameraMove01(Axis0Base + gongjiangaodu + xiashamiantutai, Axis1Base, velMax0, acc0, dec0, 1);
            if (resAbs != 1) return; //-12轴联动超范围 -22上面相机横轴不在安全位置 -10轴运动失败 -99急停
            MessageBox.Show("相机到达拍摄小端面位置");
        }

        private void btn_mulStart1btn_mulshm_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            if (ReadDI(DI["急停"]) == 0) return;
            int resAbs = UpCameraMove01(Axis0Base + xiashamiantutai + shahou, Axis1Base - wacaozhijing / 2, velMax0, acc0, dec0, 1);
            if (resAbs != 1) return;
            MessageBox.Show("相机到达拍摄上刹面位置");
        }

        private void btn_mulNQ_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            if (ReadDI(DI["急停"]) == 0) return;
            int resAbs = UpCameraMove01(Axis0Base + xiaoduanmiantutai + xiaoduanbihou, Axis1Base, velMax0, acc0, dec0, 1);
            if (resAbs != 1) return;
            MessageBox.Show("相机到达拍摄内腔面位置");
        }

        private void btn_mulStartXSM_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            if (ReadDI(DI["急停"]) == 0) return;
            int resAbs = UpCameraMove01(Axis0Base + xiaoduanmiantutai + gongjiangaodu, Axis1Base - neiyuanzhijing / 2, velMax0, acc0, dec0, 1);
            if (resAbs != 1) return;
            MessageBox.Show("相机到达拍摄下刹面位置");
        }

        private void btn_mulxdcm_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            if (ReadDI(DI["急停"]) == 0) return;
            //result234 = SideCameraMove234(Axis2Base + xiaoduanmiantutai + gongjiangaodu + 60, Axis3Baseqx + neiyuanzhijing / 2, Axis4Baseqx, velMax2, acc2, dec2, 1);
            int result234 = SideCameraMove234(Axis2Base + shahou + xiashamiantutai + 90, Axis3Baseqx - xiaoduanzhijing / 2, Axis4Baseqx, velMax2, acc2, dec2, 0);
            if (result234 != 1) return;//-345轴联动超范围 -10 轴运动失败 -99急停
            MessageBox.Show("相机到达拍摄小端侧面位置");
        }

        private void btn_mulDWY_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            if (ReadDI(DI["急停"]) == 0) return;
            int result234 = SideCameraMove234(Axis2Base + shahou + xiashamiantutai, Axis3Base - dawaiyuan / 2, Axis4Base1, velMax2, acc2, dec2, 1);
            if (result234 != 1) return;
            MessageBox.Show("相机到达拍摄大外圆位置");
        }

        private void btn_mulNQCB_Click(object sender, EventArgs e)
        {
            ReadFormula();
            SetParamMulty();
            if (ReadDI(DI["急停"]) == 0) return;
            int result234 = SideCameraMove234(Axis2Base + xiaoduanmiantutai + gongjiangaodu + 60, Axis3Baseqx + neiqiangzhijing / 2, Axis4Baseqx, velMax2, acc2, dec2, 1);
            if (result234 != 1) return;
            MessageBox.Show("相机到达拍摄内腔侧壁位置");
        }

        private void btn_mulStart5_Click(object sender, EventArgs e)
        {
            SetParamMulty();
            float distance = (float)Convert.ToDecimal(this.num_muldis5.Value);
            //判断相对位置是否超出限位
            OutLimit = false;
            JudgeMultyPos(5, distance);
            if (OutLimit == true)
            {
                //超出限位
                return;
            }
            var result = zmotion.MoveRelative(5, velMax, distance, velMin, acc, dec, sramp);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("相对运动失败" + result.ErrorMsg, "相对运动");
            }
        }



        private void btn_mulZero5_Click(object sender, EventArgs e)
        {
            SetParamMulty();
            //判断绝对位置是否超出限位
            OutLimit = false;
            JudgeAbsPos(5, 0);
            if (OutLimit == true)
            {
                //超出限位
                return;
            }
            var result = zmotion.MoveABS(5, velMax, 0, velMin, acc, dec, sramp);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("绝对运动失败" + result.ErrorMsg, "绝对运动");
            }
        }

        private void btn_mulgripwait_Click(object sender, EventArgs e)
        {

        }

        private void btn_mulflip_Click_1(object sender, EventArgs e)
        {

        }

        private void btn_mulflipP_Click(object sender, EventArgs e)
        {
            SetParamMulty();
            //判断绝对位置是否超出限位
            OutLimit = false;
            JudgeAbsPos(7, Axis7Ppos);
            if (OutLimit == true)
            {
                //超出限位
                return;
            }
            var result = zmotion.MoveABS(7, velMax, Axis7Ppos, velMin, acc, dec, sramp);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("绝对运动失败" + result.ErrorMsg, "绝对运动");
            }
        }

        private void btn_mulflipN_Click(object sender, EventArgs e)
        {
            SetParamMulty();
            //判断绝对位置是否超出限位
            OutLimit = false;
            JudgeAbsPos(7, Axis7Npos);
            if (OutLimit == true)
            {
                //超出限位
                return;
            }
            var result = zmotion.MoveABS(7, velMax, Axis7Npos, velMin, acc, dec, sramp);
            if (result.IsSuccess == false)
            {
                MessageBox.Show("绝对运动失败" + result.ErrorMsg, "绝对运动");
            }
        }


        #endregion

        #region 手动按钮
        private void btnChoice_Click(object sender, EventArgs e)
        {
            if (sender is Button btn)
            {
                if (btn.Tag != null && btn.Tag.ToString().Length > 0)
                {

                    switch (btn.Tag.ToString())
                    {
                        case "自动门开":
                            AutoDoorOpen();
                            break;
                        case "自动门关":
                            AutoDoorClose();
                            break;
                        case "手爪打开":
                            CrampOpen();
                            break;
                        case "手爪抱紧":
                            CrampClose();
                            break;
                        case "磁铁上升":
                            MagUp();
                            break;
                        case "磁铁下降":
                            MagDown();
                            break;
                        case "光源上升":
                            LightUp();
                            break;
                        case "光源下降":
                            LightDown();
                            break;
                        case "上光源开":
                            UpLightOpen();
                            break;
                        case "上光源关":
                            UpLightClose();
                            break;
                        case "侧光源开":
                            SideLightOpen();
                            break;
                        case "侧光源关":
                            SideLightClose();
                            break;
                        case "吹气":
                            Blow(2000);
                            break;
                        case "三色灯红":
                            Three_Color_RED();
                            break;
                        case "三色灯绿":
                            Three_Color_Green();
                            break;
                        case "三色灯黄":
                            Three_Color_Yellow();
                            break;
                        case "测试":
                            TestKuozhan();
                            break;


                        default:
                            break;
                    }




                }

            }
        }

        private void button33_Click(object sender, EventArgs e)
        {
            //'防止所有驱动器全部同时切换使能导致瞬间电流过大,每个轴使能后要加个延时
        }

        private void btn_Blow_MouseDown(object sender, MouseEventArgs e)
        {
            Blow(2);
        }

        private void btn_Blow_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void btn_Auto_Click(object sender, EventArgs e)
        {
            if (AxisNum != 0)
            {
                AddLog(2, "连接轴数量错误，只连接了" + AxisNum + "个轴");
                return;
            }
            else
            {
                //判断轴 有没有报警
                bool b = false;
                for (int i = 0; i < 8; i++)
                {
                    if (SDOREAD6041(0, (uint)i, 0x6041, 0, 6) == "1")
                    {
                        b = true;
                        AddLog(2, "第" + (i + 1) + "个轴驱动器有报警，请确认");
                    }
                    if (i == 7 && b == true)
                    {
                        return;
                    }
                }

            }
            if (isConnectOK == false)
            {
                AddLog(1, "轴卡未连接成功");
                return;
            }
            if (AutoMode == false && Simulate == false)
            {
                if (GetAutoState() == false) return;

            }
            AutoMode = !AutoMode;
            this.btn_Auto.BackColor = AutoMode ? Color.FromArgb(0, 192, 0) : SystemColors.ControlLight;
            this.btn_Auto.ForeColor = AutoMode ? Color.White : SystemColors.ControlText;
            this.btn_Auto.Text = AutoMode ? "自动模式" : "手动模式";
            AddLog(0, AutoMode ? "切换为自动模式" : "切换为手动模式");


        }

        /// <summary>
        /// 手动/自动模式切换后 使能开关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Auto_TextChanged(object sender, EventArgs e)
        {
            if (AutoMode)
            {
                this.grbSingle.Enabled = false;
                this.grbMove.Enabled = false;
                this.panel_Multy.Enabled = false;
                this.btn_ResetSystem.Enabled = false;
                this.btn_SystemZero.Enabled = false;
                this.panel_Base.Enabled = false;

            }
            else
            {
                this.grbSingle.Enabled = true;
                this.grbMove.Enabled = true;
                this.panel_Multy.Enabled = true;
                this.btn_ResetSystem.Enabled = true;
                this.btn_SystemZero.Enabled = true;
                this.panel_Base.Enabled = true;
            }
        }
        //急停
        private void btn_EmgStop_Click(object sender, EventArgs e)
        {
            if (isConnectOK == false)
            {
                AddLog(1, "轴卡未连接成功");
                return;
            }
            IsEmgStop = !IsEmgStop;

            this.btn_EmgStop.BackColor = IsEmgStop ? Color.Red : SystemColors.ControlLight;
            this.btn_EmgStop.ForeColor = IsEmgStop ? Color.White : SystemColors.ControlText;
            this.btn_EmgStop.Text = IsEmgStop ? "取消急停" : "系统急停";
        }

        private void btn_ResetSystem_Click(object sender, EventArgs e)
        {
            if (systemReset)
            {
                return;
            }
            int a = 0;
            systemReset = true;
            Task.Run(() =>
              {
                  a = SystemSet();
                  if (a != 1)
                  {
                      MessageBox.Show("系统复位失败");
                  }
                  systemReset = false;
              });
        }

        private void btn_AutoMode_Click(object sender, EventArgs e)
        {
            //初始化当前步骤
            if (isConnectOK == false)
            {
                AddLog(1, "轴卡未连接成功");
                return;
            }
            SystemReadytoRobot();
            if (AutoMode == false)
            {
                AddLog(1, "手动模式无法自动运行");
                return;
            }
            if (isAutoTaskRunning)
            {
                AddLog(0, "自动流程已开启，等待触发");
                return;
            }
            else
            {
                AddLog(0, "自动流程首次开启，等待触发");
            }
            //isAutoTaskRunning = true;
            ////Task.Run(() =>
            ////{
            ////    while (true && CommendMethods.bClose == false)
            ////    {
            ////        AutoRunProcess();
            ////        isAutoTaskRunning = false;
            ////        Delay(10);
            ////        IsAutoStop = false;
            ////        break;
            ////    }

            //});
        }

        private void btn_Pause_Click(object sender, EventArgs e)
        {
            if (isConnectOK == false)
            {
                AddLog(1, "轴卡未连接成功");
                return;
            }
            if (ReadDI(DI["手动门关到位1"]) == 0) return;
            IsContinue = true;
            //IsPause = !IsPause;
            //this.btn_Pause.Text = IsPause ? "取消暂停" : "暂停运行";
            //this.btn_Pause.BackColor = IsPause ? Color.Red : SystemColors.ControlLight;
            //this.btn_Pause.ForeColor = IsPause ? Color.White : SystemColors.ControlText;
        }

        private void btn_ZeroHome(object sender, EventArgs e)
        {
            if (sender is Button btn)
            {
                if (btn.Tag != null && btn.Tag.ToString().Length > 0)
                {
                    //获取轴号
                    if (isConnectOK == false)
                    {
                        AddLog(1, "轴卡未连接成功");
                        return;
                    }
                    if (AutoMode == true)
                    {
                        AddLog(1, "自动模式无法回零");
                        return;
                    }
                    DialogResult dialogResult = new FrmMsgBoxWithAck("请确认轴是否可以安全回零", "回零操作").ShowDialog();
                    if (dialogResult == DialogResult.Cancel)
                    {
                        AddLog(0, "取消回零操作");
                        return;
                    }
                    SetParamSingle();
                    OperationResult result;
                    short axis = Convert.ToInt16(btn.Tag.ToString());
                    //3正转回零，4反转回零
                    switch (axis)
                    {
                        case 0:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "上面相机竖轴1，回零失败");
                            }
                            else
                            {
                                AddLog(0, "上面相机竖轴1，回零OK");
                            }
                            break;
                        case 1:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "上面相机横轴2，回零失败");
                            }
                            {
                                AddLog(0, "上面相机横轴2，回零OK");
                            }
                            break;
                        case 2:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "侧面相机竖轴3，回零失败");
                            }
                            else
                            {
                                AddLog(0, "侧面相机竖轴3，回零OK");
                            }
                            break;
                        case 3:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "侧面相机横轴4，回零失败");
                            }
                            else
                            {
                                AddLog(0, "侧面相机横轴4，回零OK");
                            }
                            break;
                        case 4:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "侧面相机旋转轴5，回零失败");
                            }
                            {
                                AddLog(0, "侧面相机旋转轴5，回零OK");
                            }
                            break;
                        case 5:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "刹车盘旋转轴6，回零失败");
                            }
                            else
                            {
                                AddLog(0, "刹车盘旋转轴6，回零OK");
                            }
                            break;
                        case 6:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "刹车盘上升轴7，回零失败");
                            }
                            else
                            {
                                AddLog(0, "刹车盘上升轴7，回零OK");
                            }
                            break;
                        case 7:
                            result = zmotion.BusSetZero(axis);
                            if (result.IsSuccess == false)
                            {
                                AddLog(2, "刹车盘翻面轴8，回零失败");
                            }
                            else
                            {
                                AddLog(0, "刹车盘翻面轴8，回零OK");
                            }
                            break;
                        default:
                            break;
                    }




                }

            }


        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            if (isConnectOK == false)
            {
                AddLog(1, "未连接轴卡");
                return;
            }
            string[] value = new string[8];
            for (int i = 0; i < 8; i++)
            {
                value[i] = SDOREAD2001(0, (uint)i, 0x2001, 0, 4).ToUpper();
            }
            this.txt_Alarm0.Text = value[0];
            this.txt_Alarm1.Text = value[1];
            this.txt_Alarm2.Text = value[2];
            this.txt_Alarm3.Text = value[3];
            this.txt_Alarm4.Text = value[4];
            this.txt_Alarm5.Text = value[5];
            this.txt_Alarm6.Text = value[6];
            this.txt_Alarm7.Text = value[7];

        }

        //总线初始化
        private void btn_BusInit_Click(object sender, EventArgs e)
        {
            this.C_Bus_Status.Text = "初始化未完成";
            zmotion.IniFile();
        }


        private void button34_Click(object sender, EventArgs e)
        {

        }

        private void btn_Down0_Click(object sender, EventArgs e)
        {

        }

        //运行界面显示
        private void btn_Run_Click(object sender, EventArgs e)
        {
            this.tab_Monitor.SelectedIndex = 0;
        }
        //单轴调试
        private void btn_SingleDebug_Click(object sender, EventArgs e)
        {
            this.tab_Monitor.SelectedIndex = 1;
        }
        //多轴调试
        private void btn_MultyDebug_Click(object sender, EventArgs e)
        {
            this.tab_Monitor.SelectedIndex = 2;
        }
        //点位示教
        private void btn_PosTeach_Click(object sender, EventArgs e)
        {
            if (b_con)
            {
                this.tab_Monitor.SelectedIndex = 3;
                return;
            }
            DialogResult dialogResult = new FrmPassword().ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                this.tab_Monitor.SelectedIndex = 3;
                b_con = true;
                Task.Run(() =>
                {
                    Delay(300000);
                    b_con = false;
                });
            }


        }
        //系统设置
        private void btn_SysSet_Click(object sender, EventArgs e)
        {
            this.tab_Monitor.SelectedIndex = 4;
        }

        private void btn_Formula_Click(object sender, EventArgs e)
        {
            this.tab_Monitor.SelectedIndex = 5;
        }



        #endregion

        #region 临时测试
        private void btn_TestZero_Click(object sender, EventArgs e)
        {
            zmotion.BusSetZero(0);
            zmotion.BusSetZero(1);
            zmotion.BusSetZero(2);
            zmotion.BusSetZero(3);
            zmotion.BusSetZero(4);
            zmotion.BusSetZero(5);
            zmotion.BusSetZero(6);
            zmotion.BusSetZero(7);
        }

        private void btn_Forword7_Click(object sender, EventArgs e)
        {

        }



        #endregion

        #region 示教基准位置
        //上面相机位置示教
        private void btn_0Axisbase_Click(object sender, EventArgs e)
        {

            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_0Axisbase.Text = zmotion.GetPos(0).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机竖轴0", "基准位置", this.txt_0Axisbase.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上相机竖轴基准位置示教成功");
            }
        }

        private void btn_0AxisSafe_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_0AxisSafe.Text = zmotion.GetPos(0).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机竖轴0", "安全位置", this.txt_0AxisSafe.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上相机竖轴安全位置示教成功");
            }
        }


        private void btn_1Axisbase_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_1Axisbase.Text = zmotion.GetPos(1).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机横轴1", "基准位置", this.txt_1Axisbase.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上相机横轴拍照位置示教成功");
            }
        }
        private void btn_1AxisSafe_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_1AxisSafe.Text = zmotion.GetPos(1).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机横轴1", "安全位置", this.txt_1AxisSafe.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上面相机横轴安全位置示教成功");
            }
        }
        //侧面相机位置示教
        private void btn_2Axisbase_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_2Axisbase.Text = zmotion.GetPos(2).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机竖轴2", "基准位置", this.txt_2Axisbase.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机竖轴基准位置示教成功");
            }
        }

        private void btn_2AxisSafe_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_2AxisSafe.Text = zmotion.GetPos(2).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机竖轴2", "安全位置", this.txt_2AxisSafe.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机竖轴安全位置示教成功");
            }
        }

        private void btn_3Axisbase_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_3Axisbase.Text = zmotion.GetPos(3).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机横轴3", "基准位置", this.txt_3Axisbase.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机横轴基准位置示教成功");
            }
        }

        private void btn_3qxbase_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_3Axisbaseqx.Text = zmotion.GetPos(3).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机横轴3", "倾斜基准位置", this.txt_3Axisbaseqx.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机横轴基准位置示教成功");
            }
        }


        private void btn_3AxisSafe_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_3AxisSafe.Text = zmotion.GetPos(3).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机横轴3", "安全位置", this.txt_3AxisSafe.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机横轴安全位置示教成功");
            }
        }

        private void btn_4Axisbase1_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_4Axisbase1.Text = zmotion.GetPos(4).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机旋转轴4", "水平位置", this.txt_4Axisbase1.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机旋转轴水平位置示教成功");
            }
        }
        private void btn_4Axisbase2_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_4Axisbase2.Text = zmotion.GetPos(4).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机旋转轴4", "倾斜位置", this.txt_4Axisbase2.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机旋转轴倾斜位置示教成功");
            }
        }

        private void btn_4AxisSafe_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_4AxisSafe.Text = zmotion.GetPos(4).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机旋转轴4", "安全位置", this.txt_4AxisSafe.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机旋转轴安全位置示教成功");
            }
        }


        //旋转轴5位置示教
        private void btn_5AxisPos_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_5AxisPos.Text = zmotion.GetPos(5).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘旋转轴5", "基准位置", this.txt_5AxisPos.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("5轴示教成功");
            }
        }

        //上升轴6等待位置示教
        private void btn_6AxisWait_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_6AxisWait.Text = zmotion.GetPos(6).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘上升轴6", "等待位置", this.txt_6AxisWait.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("等待位置示教成功");
            }
        }

        private void btn_6AxisDown_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_6AxisDown.Text = zmotion.GetPos(6).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘上升轴6", "下降基准位置", this.txt_6AxisDown.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("下降基准位置示教成功");
            }
        }

        private void btn_6AxisGripbase_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_6AxisGripBase.Text = zmotion.GetPos(6).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘上升轴6", "抓取基准位置", this.txt_6AxisGripBase.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("抓取基准位置示教成功");
            }
        }

        private void btn_6AxisUp_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_6AxisUp.Text = zmotion.GetPos(6).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘上升轴6", "上升基准位置", this.txt_6AxisUp.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上升基准位置示教成功");
            }
        }
        //翻面轴位置示教
        private void btn_7AxisP_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_7AxisPpos.Text = zmotion.GetPos(7).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘翻面轴7", "正转位置", this.txt_7AxisPpos.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("正转位置示教成功");
            }
        }

        private void btn_7AxisN_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_7AxisNpos.Text = zmotion.GetPos(7).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("刹车盘翻面轴7", "反转位置", this.txt_7AxisNpos.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("反转位置示教成功");
            }
        }
        //1轴极限位置示教
        private void btn_0limitA_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_0AxisLimitup.Text = zmotion.GetPos(0).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机竖轴0", "上方极限位置", this.txt_0AxisLimitup.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上面相机竖轴上方极限位置示教成功");
            }
        }

        private void btn_0limitB_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_0AxisLimitdown.Text = zmotion.GetPos(0).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机竖轴0", "下方极限位置", this.txt_0AxisLimitdown.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上面相机竖轴下方极限位置示教成功");
            }
        }
        //2轴极限位置示教
        private void btn_1imitA_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_1AxisLimitout.Text = zmotion.GetPos(1).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机横轴1", "远离极限位置", this.txt_1AxisLimitout.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上面相机横轴远离极限位置示教成功");
            }
        }

        private void btn_1limitB_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_1AxisLimitin.Text = zmotion.GetPos(1).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("上面相机横轴1", "靠近极限位置", this.txt_1AxisLimitin.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("上面相机横轴靠近极限位置示教成功");
            }
        }
        //3轴极限位置示教
        private void btn_2limitA_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_2AxisLimitup.Text = zmotion.GetPos(2).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机竖轴2", "上方极限位置", this.txt_2AxisLimitup.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机竖轴上方极限位置示教成功");
            }
        }

        private void btn_2limitB_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_2AxisLimitdown.Text = zmotion.GetPos(2).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机竖轴2", "下方极限位置", this.txt_2AxisLimitdown.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机竖轴下方极限位置示教成功");
            }
        }
        //4轴极限位置示教
        private void btn_3limitA_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_3AxisLimitout.Text = zmotion.GetPos(3).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机横轴3", "远离极限位置", this.txt_3AxisLimitout.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机横轴远离极限位置示教成功");
            }
        }

        private void btn_3limitB_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PosBasePath))
            {
                FileStream fs = new FileStream(CommendMethods.PosBasePath, FileMode.Create);
                fs.Close();
            }
            bool result = true;
            this.txt_3AxisLimitin.Text = zmotion.GetPos(3).ToString("f1");

            //运动参数
            result = IniConfigHelper.WriteIniData("侧面相机横轴3", "靠近极限位置", this.txt_3AxisLimitin.Text.ToString(), CommendMethods.PosBasePath);
            if (result)
            {
                MessageBox.Show("侧面相机横轴靠近极限位置示教成功");
            }
        }





        #endregion

        #region 配方通用方法
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int ReadFormula()
        {
            gongjiangaodu = (float)this.num_gongjiangaodu.Value;
            xiaoduanmiantutai = (float)this.num_xiaoduantutai.Value;
            xiashamiantutai = (float)this.num_xiashamiantutai.Value;
            xiaoduangaodu = (float)this.num_xiaoduangaodu.Value;
            shahou = (float)this.num_shahou.Value;
            neiqiangshendu = (float)this.num_neiqiangshendu.Value;
            xiaoduanbihou = (float)this.num_xiaoduanbihou.Value;
            xiaoduanzhijing = (float)this.num_xiaoduanzhijing.Value;
            zhongkongzhijing = (float)this.num_zhongkongzhijing.Value;
            dawaiyuan = (float)this.num_dawaiyuan.Value;
            neiyuanzhijing = (float)this.num_neiyuanzhijing.Value;
            wacaozhijing = (float)this.num_wacaozhijing.Value;
            neiqiangzhijing = (float)this.num_neiqiangzhijing.Value;
            if (gongjiangaodu == 0 || shahou == 0 || dawaiyuan == 0 || xiaoduanbihou == 0)
            {
                MessageBox.Show("配方错误，请确认");
                return -1;
            }
            return 1;

        }



        private void btn_SaveData_Click(object sender, EventArgs e)
        {
            if (SaveIniConfig())
            {
                MessageBox.Show("保存配置成功");
            }
            else
            {
                MessageBox.Show("保存配置失败");
            }
        }

        private void num_unit4_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btn_ProcessStart_Click(object sender, EventArgs e)
        {
            if (isManuTaskRunning)
            {
                MessageBox.Show("流程已开始");
                return;
            }
            isManuTaskRunning = true;
            this.check_Run.Checked = false;
            Task.Run(() =>
            {
                int a = Test();
                ReadAlarm(a);
                isManuTaskRunning = false;
            });






        }



        private int Test()
        {
            if (this.check_Run.Checked==false)
            {
                DialogResult dialogResult = new FrmMsgBoxWithAck("请确认配方是否正确", "配方确认").ShowDialog();
                if (dialogResult != DialogResult.OK)
                {
                    return -1;
                }
            }
            
            int formulaResult = ReadFormula();
            if (formulaResult != 1) return -11;//配方错误

            bool b_result = JudgeSafePos(7);
            if (b_result) return -2;//7轴不在安全位置

            #region //局部变量定义
            int resmul = 0;
            int resAbs = 0;
            #endregion

            #region //流程启动后，先关闭门，升磁铁
            if (ReadDI(DI["急停"]) == 0) return -99;//急停按钮
            UpLightOpen();
            AutoDoorClose();
            int lightResult = LightUp();
            if (lightResult != 1) return lightResult;
            #endregion

            //侧面相机可以提前准备好  不用乱开多任务，在位置判断即可
            #region //侧面相机拍小端侧壁
            if (ReadDI(DI["急停"]) == 0) return -99;
            //result234 = SideCameraMove234(Axis2Base + xiaoduanmiantutai + gongjiangaodu + 60, Axis3Baseqx + neiyuanzhijing / 2, Axis4Baseqx, velMax2, acc2, dec2, 1);
            int result234 = SideCameraMove234(Axis2Base + shahou + xiashamiantutai + 90, Axis3Baseqx - xiaoduanzhijing / 2, Axis4Baseqx, velMax2, acc2, dec2, 0);
            if (result234 != 1) return result234;//-345轴联动超范围 -10 轴运动失败 -99急停 

            #endregion

            #region  复夹一下
            resAbs = UpCameraMove01(Axis0Base + gongjiangaodu + xiashamiantutai, Axis1Base, velMax0, acc0, dec0, 0);
            int resAbs1 = Abs(6, Axis6Down, velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs1 != 1) return resAbs1;
            CrampClose();
            Delay(2000);
            MagUp();
            Delay(1000);
            CrampOpen();
            Delay(2000);
            if (resAbs1 != 1) return resAbs1;
            #endregion

            #region //拍小端面
            //吹气
            //Blow(2000);
            if (ReadDI(DI["急停"]) == 0) return -99;
            int signal = WaitDigital("自动门关到位");
            if (signal != 1) return signal;
            JudgeAxisStop(0,1);
            Delay(500);

            #endregion


            #region //拍上刹面 同时拍小端侧壁
            if (ReadDI(DI["急停"]) == 0) return -99;
            signal = WaitDigital("磁铁上升到位");
            if (signal != 1) return signal;
            signal = WaitDigital("光源上升到位");
            if (signal != 1) return signal;
            SideLightOpen();
            if (wacaozhijing == 0)
            {
                wacaozhijing = xiaoduanzhijing;
            }
            resAbs = UpCameraMove01(Axis0Base + xiashamiantutai + shahou, Axis1Base - wacaozhijing / 2, velMax0, acc0, dec0, 1);
            if (resAbs != 1) return resAbs;
            JudgeAxisStop(2, 3, 4);
            Delay(200);
            for (int i = 0; i < 7; i++)
            {
                resmul = Multy(5, 60, velMax5, velMin5, acc5, dec5, sramp5, 1);
                if (resmul != 1) return resmul; // -Axis轴超范围 -10 轴运动失败 -99急停
                Delay(200);
            }

            #endregion

            #region//上面相机退到安全位置
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = UpCameraMove01(Axis0Safe, Axis1Safe, velMax0, acc0, dec0, 0);
            if (resAbs != 1) return resAbs;
            #endregion
         
            #region //侧面相机拍大外圆侧壁
            if (ReadDI(DI["急停"]) == 0) return -99;
            result234 = SideCameraMove234(Axis2Base + shahou / 2 + xiashamiantutai, Axis3Base - dawaiyuan / 2, Axis4Base1, velMax2, acc2, dec2, 1);
            if (result234 != 1) return result234;
            Delay(200);
            for (int i = 0; i < 7; i++)
            {
                resmul = Multy(5, -60, velMax5, velMin5, acc5, dec5, sramp5, 1);
                if (resmul != 1) return resmul;
                Delay(200);
            }
            #endregion

            #region //侧面光源下降
            SideLightClose();
            LightDown();
            if (ReadDI(DI["急停"]) == 0) return -99;

            #endregion

            #region //上升轴先到等待抓取位置 圆滑过渡
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = Abs(6, Axis6Down, velMax6, velMin6, acc6, dec6, sramp6, 0);
            if (resAbs != 1) return resAbs;
            #endregion

            #region //侧面相机退到安全位置
            if (ReadDI(DI["急停"]) == 0) return -99;
            result234 = SideCameraMove234(Axis2Safe, Axis3Safe, Axis4Safe, velMax2, acc2, dec2, 0);
            if (result234 != 1) return result234;
            #endregion

            #region //刹车盘转到0度位置
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = Abs(5, 0, velMax5, velMin5, acc5, dec5, sramp5, 1);
            if (resAbs != 1) return resAbs;
            #endregion

            //判断上升轴是否到位、判断相机是否在安全位置、以及旋转轴的位置
            #region //刹车盘翻面
            if (ReadDI(DI["急停"]) == 0) return -99;
            MagDown();
            //判断轴是否停止
            JudgeAxisStop(0, 1);
            //侧面相机拍完不需要判断是否在安全位置
            //JudgeAxisStop(2, 3, 4);
            JudgeAxisStop(5);
            //判断轴是否在安全位置
            bool b_result1 = JudgeSafePos(0);
            bool b_result2 = JudgeSafePos(1);
            //b_result &= JudgeSafePos(2);
            //b_result &= JudgeSafePos(3);
            //b_result &= JudgeSafePos(4);
            if (b_result1) return -1;
            if (b_result2) return -2;


            signal = WaitDigital("磁铁下降到位");
            if (signal != 1) return signal;
            //signal = WaitDigital("光源下降到位");
            //if (signal != 1) return signal;

            CrampClose();
            Delay(2000);
            resAbs = Abs(6, Axis6Up, velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs != 1) return resAbs;
            //判断下气爪状态，如果夹紧信号有，说明工件未抓起
            if (ReadDI(DI["手爪抱紧到位"]) == 1)
            {
                return -100;
            }
            //吹气
            Blow(2000);
            resAbs = Abs(5, 360, velMax5, velMin5, acc5, dec5, sramp5, 0);
            if (resAbs != 1) return resAbs;
            resAbs = Abs(7, Axis7Npos, velMax7, velMin7, acc7, dec7, sramp7, 1);
            if (resAbs != 1) return resAbs;
            //-64.4
            JudgeAxisStop(5);
            resAbs = Abs(6, Axis6Down - (xiashamiantutai + gongjiangaodu + xiaoduanmiantutai - 50 + Math.Abs(Axis6Down - Axis6GripBase) * 2) - 2, velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs != 1) return resAbs;

            CrampOpen();
            Delay(1000);
            //复夹位置
            resAbs = Abs(6, Axis6Down - (xiashamiantutai + gongjiangaodu + xiaoduanmiantutai - 50 + Math.Abs(Axis6Down - Axis6GripBase) * 2), velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs != 1) return resAbs;
            //上面相机提前运动到拍照位置
            resAbs = UpCameraMove01(Axis0Base + xiaoduanmiantutai + gongjiangaodu, Axis1Base - neiyuanzhijing / 2, velMax0, acc0, dec0, 0);
            //侧面相机提前运动到拍照位置
            result234 = SideCameraMove234(Axis2Base + xiaoduanmiantutai + gongjiangaodu + 60, Axis3Baseqx + neiqiangzhijing / 2, Axis4Baseqx, velMax2, acc2, dec2, 1);
            if (resAbs != 1) return resAbs;
            if (result234 != 1) return result234;
            //复夹一下
            CrampClose();
            Delay(1000);
            MagUp();
            Delay(2000);
            CrampOpen();
            signal = WaitDigital("手爪打开到位");
            if (signal != 1) return signal;
            signal = WaitDigital("磁铁上升到位");
            if (signal != 1) return signal;
            #endregion

            #region //先拍下刹面和内腔侧壁一起
            if (ReadDI(DI["急停"]) == 0) return -99;
            if (neiyuanzhijing == 0)
            {
                neiyuanzhijing = neiqiangzhijing;
            }
            //判断轴是否停止
            JudgeAxisStop(0, 1);
            JudgeAxisStop(2, 3,4);
            Delay(200);
            for (int i = 0; i < 7; i++)
            {
                resmul = Multy(5, -60, velMax5, velMin5, acc5, dec5, sramp5, 1);
                if (resmul != 1) return resmul;
                Delay(200);
            }
            #endregion

            #region //侧面相机退到安全位置
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = SideCameraMove234(Axis2Safe, Axis3Safe, Axis4Safe, velMax2, acc2, dec2, 0);
            if (resAbs != 1) return resAbs;

            #endregion


            //判断上面相机是否到位
            #region //最后拍内腔
            resAbs = UpCameraMove01(Axis0Base + xiaoduanmiantutai + xiaoduanbihou, Axis1Base, velMax0, acc0, dec0, 1);
            if (resAbs != 1) return resAbs;
            if (ReadDI(DI["急停"]) == 0) return -99;
            //判断轴是否停止
            JudgeAxisStop(0, 1);
            Delay(500);
            #endregion

            #region//上面相机退到安全位置
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = UpCameraMove01(Axis0Safe, Axis1Safe, velMax0, acc0, dec0, 0);
            if (resAbs != 1) return resAbs;
            #endregion

 

            #region //上升轴先到等待抓取位置 圆滑过渡
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = Abs(6, Axis6Down - (xiashamiantutai + gongjiangaodu + xiaoduanmiantutai - 50 + Math.Abs(Axis6Down - Axis6GripBase) * 2), velMax6, velMin6, acc6, dec6, sramp6, 0);
            if (resAbs != 1) return resAbs;
            #endregion

            #region //刹车盘转到0度位置
            if (ReadDI(DI["急停"]) == 0) return -99;
            resAbs = Abs(5, 0, velMax5, velMin5, acc5, dec5, sramp5, 1);
            if (resAbs != 1) return resAbs;
            #endregion

            #region //刹车盘检完翻面
            if (ReadDI(DI["急停"]) == 0) return -99;
            MagDown();
            //判断轴是否在安全位置
            JudgeAxisStop(0, 1);
            //c侧面相机拍完不需要判断是否在安全位置
            JudgeAxisStop(2, 3, 4);
            JudgeAxisStop(5);
            JudgeAxisStop(6);
            b_result1 = JudgeSafePos(0);
            b_result2 = JudgeSafePos(1);
            bool b_result3 = JudgeSafePos(2);
            bool b_result4 = JudgeSafePos(3);
            bool b_result5 = JudgeSafePos(4);
            if (b_result1) return -1;
            if (b_result2) return -2;
            if (b_result3) return -3;
            if (b_result4) return -4;
            if (b_result5) return -5;

            signal = WaitDigital("磁铁下降到位");
            if (signal != 1) return signal;
            signal = WaitDigital("光源下降到位");
            if (signal != 1) return signal;

            CrampClose();
            Delay(2000);
            resAbs = Abs(6, Axis6Up, velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs != 1) return resAbs;
            resAbs = Abs(7, Axis7Ppos, velMax7, velMin7, acc7, dec7, sramp7, 1);
            if (resAbs != 1) return resAbs;
            resAbs = Abs(6, Axis6Down, velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs != 1) return resAbs;

            CrampOpen();
            Delay(1000);
            //CrampClose();
            //Delay(2000);
            //CrampOpen();
            signal = WaitDigital("手爪打开到位");
            if (signal != 1) return signal;
            resAbs = Abs(6, Axis6Wait, velMax6, velMin6, acc6, dec6, sramp6, 1);
            if (resAbs != 1) return resAbs;
            if (ReadDI(DI["急停"]) == 0) return -99;
            #endregion


            AutoDoorOpen();
            JudgeAxisStop(0, 1);
            JudgeAxisStop(2, 3, 4);
            UpLightClose();
            return 1;

        }

        private void WaitOut(object sender, ElapsedEventArgs e)
        {
            WaitOutTime = true;
        }

        private void ManualDoor()
        {
            if (ReadDI(DI["手动门关到位1"]) == 0)
            {
                AddLog(1, "自动运行过程中，手动门开");
                do
                {

                } while (!(IsContinue && ReadDI(DI["手动门关到位1"]) == 1));
                AddLog(0, "流程继续执行");
            }
            IsContinue = false;
        }


        /// <summary>
        /// 单轴绝对运动，判断位置是否可达
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <param name="pos">位置</param>
        /// <param name="velMax">运行速度</param>
        /// <param name="velMin">起始速度0</param>
        /// <param name="acc">加速度</param>
        /// <param name="dec">减速度</param>
        /// <param name="sramp">S曲线</param>
        /// <param name="status">运动类型 1是精确到达，0是圆滑过渡</param>
        /// <returns>返回 true 表示运动失败 </returns>
        private int Abs(short axis, float pos, float velMax, float velMin, float acc, float dec, float sramp, float status)
        {
            if (ReadDI(DI["急停"]) == 1)
            {
                if (axis == 0)
                {
                    //0轴运动，需要先判断下6轴在不在安全位置
                    if (JudgeSafePos(6))
                    {
                        return -7;
                    }
                }
                //判断相对位置是否超出限位
                OutLimit = false;
                JudgeAbsPos(axis, pos);
                if (OutLimit == true)
                {
                    AddLog(1, axis + "轴绝对位置超范围");
                    return -(axis + 1);
                }
                var result = zmotion.MoveABS(axis, velMax, pos, velMin, acc, dec, sramp);
                Delay(100);
                if (result.IsSuccess == false)
                {
                    AddLog(1, axis + "轴运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                    return -10;
                }
                if (status == 1)
                {
                    JudgeAxisStop(axis);
                }

                return 1;
            }
            else
            {
                return -99;
            }

        }

        //单轴相对运动，判断位置是否可达
        private int Multy(short axis, float pos, float velMax, float velMin, float acc, float dec, float sramp, float status)
        {
            if (ReadDI(DI["急停"]) == 1)
            {
                //判断相对位置是否超出限位
                OutLimit = false;
                JudgeMultyPos(axis, pos);
                if (OutLimit == true)
                {
                    AddLog(1, axis + "轴相对位置超范围");
                    return -(axis + 1);
                }
                var result = zmotion.MoveRelative(axis, velMax, pos, velMin, acc, dec, sramp);
                Delay(100);
                if (result.IsSuccess == false)
                {
                    AddLog(1, axis + "轴运动失败" + result.ErrorMsg);
                    IsAutoStop = true;
                    return -10;
                }
                if (status == 1)
                {
                    JudgeAxisStop(axis);
                }
                return 1;
            }
            else
            {
                return -99;
            }

        }
        /// <summary>
        /// 判断轴是否在安全位置
        /// </summary>
        /// <param name="axis">轴号</param>
        /// <returns>返回True，表示不在安全位置，返回false，表示在安全位置</returns>
        private bool JudgeSafePos(short axis)
        {
            float pos = (zmotion.GetMos(axis));
            float limit = 2;
            switch (axis)
            {
                case 0:
                    if (pos < Axis0Safe - limit || pos > Axis0Safe + limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("1轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 1:
                    if (pos < Axis1Safe - limit || pos > Axis1Safe + limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("2轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 2:
                    if (pos < Axis2Safe - limit || pos > Axis2Safe + limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("3轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 3:
                    if (pos < Axis3Safe - limit || pos > Axis3Safe + limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("4轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 4:
                    if (pos < Axis4Safe - limit || pos > Axis4Safe + limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("5轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 5:
                    if (pos < 0 - limit || pos > 0 + limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("6轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 6:
                    if (pos < Axis6Down - limit)
                    {
                        OutLimit = true;
                        MessageBox.Show("7轴不在安全位置，请确认");
                        return true;
                    }
                    break;
                case 7:
                    if ((pos < Axis7Ppos - limit || pos > Axis7Ppos + limit))
                    {
                        OutLimit = true;
                        MessageBox.Show("8轴不在安全位置，请确认");
                        return true;
                    }
                    break;

                default:
                    break;
            }
            return false;


        }

        /// <summary>
        /// 告诉机械手 设备准备OK
        /// </summary>
        /// <returns></returns>
        private int SystemReadytoRobot()
        {
            if (ReadDI(DI["自动门开到位"]) != 1)
            {
                //自动门开不到位
                return -31;
            }
            if (ReadDI(DI["手爪打开到位"]) != 1)
            {
                //手爪打开不到位
                return -34;
            }
            if (ReadDI(DI["磁铁下降到位"]) != 1)
            {
                //磁铁下降不到位
                return -36;
            }
            if (ReadDI(DI["光源下降到位"]) != 1)
            {
                //光源下降不到位
                return -40;
            }
            if (ReadDI(DI["手动门关到位1"]) != 1)
            {
                //手动门不到位
                return -41;
            }
            if (JudgeSafePos(0))
            {
                //上面相机竖轴不在安全位置
                return -1;
            }
            if (JudgeSafePos(1))
            {
                //上面相机横轴不在安全位置
                return -2;
            }
            if (JudgeSafePos(2))
            {
                //侧面相机竖轴不在安全位置
                return -3;
            }
            if (JudgeSafePos(3))
            {
                //侧面相机横轴不在安全位置
                return -4;
            }
            if (JudgeSafePos(4))
            {
                //侧面相机旋转轴不在安全位置
                return -5;
            }
            if (JudgeSafePos(5))
            {
                //刹车盘旋转轴不在安全位置
                return -6;
            }
            if (JudgeSafePos(6))
            {
                //上升轴不在安全位置
                return -7;
            }
            if (JudgeSafePos(7))
            {
                //翻面轴不在安全位置
                return -8;
            }
            if (ReadDI(DI["急停"]) == 0)
            {
                //急停按钮按下
                return -99;
            }

            return 1;


        }
        /// <summary>
        /// 系统复位
        /// </summary>
        /// <returns></returns>
        private int SystemSet()
        {
            if (isConnectOK == false)
            {
                AddLog(1, "轴卡未连接成功");
                return -1;
            }
            zmotion.AlarmReset(upAxis0);
            zmotion.AlarmReset(upAxis1);
            zmotion.AlarmReset(sideAxis1);
            zmotion.AlarmReset(sideAxis2);
            zmotion.AlarmReset(sideAxis3);
            zmotion.AlarmReset(rotateAxis);
            zmotion.AlarmReset(updownAxis);
            zmotion.AlarmReset(flipAxis);
            if (ReadDI(DI["自动门开到位"]) != 1)
            {
                AutoDoorOpen();
            }
            if (ReadDI(DI["手爪打开到位"]) != 1)
            {
                CrampOpen();
            }
            if (ReadDI(DI["磁铁下降到位"]) != 1)
            {
                MagDown();
            }
            if (ReadDI(DI["光源下降到位"]) != 1)
            {
                LightDown();
            }

            if (JudgeSafePos(2))
            {
                int resAbs = Abs(2, Axis2Safe, 100, velMin2, acc2, dec2, sramp2, 1);
                if (resAbs != 1)
                {
                    return -1;
                }
            }
            if (JudgeSafePos(3))
            {
                int resAbs = Abs(3, Axis3Safe, 100, velMin3, acc3, dec3, sramp3, 1);
                if (resAbs != 1)
                {
                    return -1;
                }
            }
            if (JudgeSafePos(4))
            {
                int resAbs = Abs(4, Axis4Safe, 100, velMin4, acc4, dec4, sramp4, 1);
                if (resAbs != 1)
                {
                    return -1;
                }
            }

            if (JudgeSafePos(1))
            {
                //先回横轴
                int resAbs = Abs(1, Axis1Safe, 100, velMin1, acc1, dec1, sramp1, 1);
                if (resAbs != 1)
                {
                    return -1;
                }
            }

            if (JudgeSafePos(0))
            {
                int resAbs = Abs(0, Axis0Safe, 100, velMin0, acc0, dec0, sramp0, 1);
                if (resAbs != 1)
                {
                    return -1;
                }
            }


            if (JudgeSafePos(5))
            {
                MagUp();
                WaitDigital("磁铁上升到位");
                int resAbs = Abs(5, 0, 100, velMin5, acc5, dec5, sramp5, 1);
                MagDown();
                if (resAbs != 1)
                {
                    return -1;
                }
            }

            if (JudgeSafePos(7))
            {
                int resAbs = Abs(6, Axis6Up, 100, velMin6, acc6, dec6, sramp6, 1);
                if (resAbs != 1) return resAbs;
                CrampClose();
                int result = WaitDigital("手爪抱紧到位");
                if (result != 1) return result;
                resAbs = Abs(7, Axis7Ppos, 100, velMin7, acc7, dec7, sramp7, 1);
                if (resAbs != 1) return resAbs;
                CrampOpen();
                result = WaitDigital("手爪打开到位");
                if (result != 1) return result;
                resAbs = Abs(6, 0, 100, velMin6, acc6, dec6, sramp6, 1);
                if (resAbs != 1) return resAbs;
            }

            if (zmotion.GetMos(6) != 0)
            {
                int resAbs = Abs(6, 0, 100, velMin6, acc6, dec6, sramp6, 1);
                if (resAbs != 1) return resAbs;
            }

            if (JudgeSafePos(0) || JudgeSafePos(1) || JudgeSafePos(2) || JudgeSafePos(3) || JudgeSafePos(4) || JudgeSafePos(5) || JudgeSafePos(6) || JudgeSafePos(7))
            {
                return -43;
            }

            if (ReadDI(DI["急停"]) == 0)
            {
                //急停按钮按下
                return -14;
            }

            MessageBox.Show("系统复位完成");
            return 1;
        }


        //判断报警原因
        private void ReadAlarm(int a)
        {
            switch (a)
            {
                case 1:
                    AddLog(0, "执行完毕");
                    break;
                case -1:
                    AddLog(2, "1轴超范围");
                    break;
                case -2:
                    AddLog(2, "2轴超范围");
                    break;
                case -3:
                    AddLog(2, "3轴超范围");
                    break;
                case -4:
                    AddLog(2, "4轴超范围");
                    break;
                case -5:
                    AddLog(2, "5轴超范围");
                    break;
                case -6:
                    AddLog(2, "6轴超范围");
                    break;
                case -7:
                    AddLog(2, "7轴超范围");
                    break;
                case -8:
                    AddLog(2, "8轴超范围");
                    break;
                case -10:
                    AddLog(2, "轴运动失败");
                    break;
                case -11:
                    AddLog(2, "配方错误");
                    break;
                case -12:
                    AddLog(2, "12轴联动超范围");
                    break;
                case -345:
                    AddLog(2, "345轴联动超范围");
                    break;
                case -13:
                    AddLog(2, "控制器连接失败");
                    break;
                case -14:
                    AddLog(2, "控制器初始化失败");
                    break;
                case -15:
                    AddLog(2, "控制器断开连接失败");
                    break;
                case -16:
                    AddLog(2, "翻面参数错误");
                    break;
                case -17:
                    AddLog(2, "流程错误");
                    break;
                case -21:
                    AddLog(2, "1轴不在安全位置");
                    break;
                case -22:
                    AddLog(2, "2轴不在安全位置");
                    break;
                case -23:
                    AddLog(2, "3轴不在安全位置");
                    break;
                case -24:
                    AddLog(2, "4轴不在安全位置");
                    break;
                case -25:
                    AddLog(2, "5轴不在安全位置");
                    break;
                case -26:
                    AddLog(2, "6轴不在安全位置");
                    break;
                case -27:
                    AddLog(2, "7轴不在安全位置");
                    break;
                case -28:
                    AddLog(2, "8轴不在安全位置");
                    break;
                case -31:
                    AddLog(2, "自动门开不到位");
                    break;
                case -32:
                    AddLog(2, "自动门关不到位");
                    break;
                case -33:
                    AddLog(2, "手爪抱紧不到位");
                    break;
                case -34:
                    AddLog(2, "手爪打开不到位");
                    break;
                case -35:
                    AddLog(2, "磁铁上升不到位");
                    break;
                case -36:
                    AddLog(2, "磁铁下降不到位");
                    break;
                case -37:
                    AddLog(2, "翻转轴正转不到位");
                    break;
                case -38:
                    AddLog(2, "翻转轴反转不到位");
                    break;
                case -39:
                    AddLog(2, "光源上升不到位");
                    break;
                case -40:
                    AddLog(2, "光源下降不到位");
                    break;
                case -41:
                    AddLog(2, "手动门关不到位1");
                    break;
                case -42:
                    AddLog(2, "手动门关不到位2");
                    break;
                case -43:
                    AddLog(2, "系统复位失败");
                    break;
                case -99:
                    AddLog(2, "急停按下");
                    break;
                default:
                    break;
            }
        }











        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <returns>返回结果</returns>
        private bool SaveIniConfig()
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.SysSetPath))
            {
                FileStream fs = new FileStream(CommendMethods.SysSetPath, FileMode.Create);
                fs.Close();
            }
            bool result = true;

            //result &= IniConfigHelper.WriteIniData("系统IP", "IP地址", this.txt_IpAdress.Text.ToString(), CommendMethods.SysSetPath);
            #region 保存配置文件
            //运动参数
            result &= IniConfigHelper.WriteIniData("上面相机竖轴0", "脉冲当量", this.num_unit00.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机竖轴0", "起始速度", this.num_velmin0.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机竖轴0", "运行速度", this.num_velmax0.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机竖轴0", "加速度", this.num_acc0.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机竖轴0", "减速度", this.num_dec0.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机竖轴0", "S曲线时间", this.num_sramp0.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("上面相机横轴1", "脉冲当量", this.num_unit11.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机横轴1", "起始速度", this.num_velmin1.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机横轴1", "运行速度", this.num_velmax1.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机横轴1", "加速度", this.num_acc1.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机横轴1", "减速度", this.num_dec1.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("上面相机横轴1", "S曲线时间", this.num_sramp1.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("侧面相机竖轴2", "脉冲当量", this.num_unit22.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机竖轴2", "起始速度", this.num_velmin2.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机竖轴2", "运行速度", this.num_velmax2.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机竖轴2", "加速度", this.num_acc2.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机竖轴2", "减速度", this.num_dec2.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机竖轴2", "S曲线时间", this.num_sramp2.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("侧面相机横轴3", "脉冲当量", this.num_unit33.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机横轴3", "起始速度", this.num_velmin3.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机横轴3", "运行速度", this.num_velmax3.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机横轴3", "加速度", this.num_acc3.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机横轴3", "减速度", this.num_dec3.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机横轴3", "S曲线时间", this.num_sramp3.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("侧面相机旋转轴4", "脉冲当量", this.num_unit44.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机旋转轴4", "起始速度", this.num_velmin4.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机旋转轴4", "运行速度", this.num_velmax4.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机旋转轴4", "加速度", this.num_acc4.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机旋转轴4", "减速度", this.num_dec4.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("侧面相机旋转轴4", "S曲线时间", this.num_sramp4.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("刹车盘旋转轴5", "脉冲当量", this.num_unit55.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘旋转轴5", "起始速度", this.num_velmin5.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘旋转轴5", "运行速度", this.num_velmax5.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘旋转轴5", "加速度", this.num_acc5.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘旋转轴5", "减速度", this.num_dec5.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘旋转轴5", "S曲线时间", this.num_sramp5.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("刹车盘上升轴6", "脉冲当量", this.num_unit66.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘上升轴6", "起始速度", this.num_velmin6.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘上升轴6", "运行速度", this.num_velmax6.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘上升轴6", "加速度", this.num_acc6.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘上升轴6", "减速度", this.num_dec6.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘上升轴6", "S曲线时间", this.num_sramp6.Value.ToString(), CommendMethods.SysSetPath);

            result &= IniConfigHelper.WriteIniData("刹车盘翻面轴7", "脉冲当量", this.num_unit77.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘翻面轴7", "起始速度", this.num_velmin7.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘翻面轴7", "运行速度", this.num_velmax7.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘翻面轴7", "加速度", this.num_acc7.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘翻面轴7", "减速度", this.num_dec7.Value.ToString(), CommendMethods.SysSetPath);
            result &= IniConfigHelper.WriteIniData("刹车盘翻面轴7", "S曲线时间", this.num_sramp7.Value.ToString(), CommendMethods.SysSetPath);


            return result;
            #endregion

        }

        private bool SavePlcConfig()
        {
            //判断文件是否存在
            if (!File.Exists(CommendMethods.PlcSetPath))
            {
                FileStream fs = new FileStream(CommendMethods.SysSetPath, FileMode.Create);
                fs.Close();
            }
            bool result = true;

            #region 保存配置文件
            //PLC选择
            //1表示FX5U，2表示西门子
            result &= IniConfigHelper.WriteIniData("PLC型号", "PLC", this.rdo_sanling.Checked ? "1" : "2", CommendMethods.PlcSetPath);
            //运动参数
            result &= IniConfigHelper.WriteIniData("PLC到设备", "设备启动地址", this.txt_StartMachine.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("PLC到设备", "Robot请求进入", this.txt_RobotCall.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("PLC到设备", "Robot已进入", this.txt_RobotIn.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("PLC到设备", "型号前缀", this.txt_Prefix.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("PLC到设备", "型号", this.txt_Model.Text.ToString().Trim(), CommendMethods.PlcSetPath);


            result &= IniConfigHelper.WriteIniData("设备到PLC", "设备准备OK", this.txt_MachineOK.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("设备到PLC", "设备已启动", this.txt_MachineRun.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("设备到PLC", "检测OK", this.txt_CheckOK.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("设备到PLC", "检测NG", this.txt_CheckNG.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("设备到PLC", "位置度OK", this.txt_PosOK.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("设备到PLC", "位置度NG", this.txt_PosNG.Text.ToString().Trim(), CommendMethods.PlcSetPath);

            result &= IniConfigHelper.WriteIniData("地址及端口", "IP地址", this.txt_PlcIP.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("地址及端口", "端口号", this.txt_PlcPort.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            result &= IniConfigHelper.WriteIniData("心跳地址", "心跳", this.txt_heart.Text.ToString().Trim(), CommendMethods.PlcSetPath);
            return result;
            #endregion

        }


        #endregion

        #region 更新运动步骤

        private void UpdateStep(string info)
        {
            Invoke(new Action(() =>
            {
                this.txt_State.Text = info;
            }));
        }

        #endregion

        #region 自动运行流程
        int dd = 0;
        private void AutoRunProcess()
        {
            CurrentStep = RunStep.RunReady;

            bool fRun = false;

            bool fOver = false;

            int Error = 0;

            int CycleTimes = 0;
            //SetParam();
            while (true && CommendMethods.bClose == false)
            {

                if (AutoMode == false)
                {
                    AddLog(1, "自动模式切换为手动模式，结束流程");
                    return;
                }
                //检测是否按下启动信号
                if (getPN.GetP(ReadDI(DI["启动"]) == 1))
                {
                    fRun = true;
                    CurrentStep = RunStep.RunReady;
                }
                if (fRun)
                {

                    fRun = false;

                    //while (true && CommendMethods.bClose == false)
                    //{
                    //    switch (CurrentStep)
                    //    {
                    //        case RunStep.RunReady:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //上面光源打开
                    //            UpLightOpen();
                    //            //磁铁上升
                    //            MagUp();
                    //            //自动门关闭
                    //            AutoDoorClose();
                    //            UpdateStep("等待磁铁上升到位13");
                    //            WaitDigital("磁铁上升到位");
                    //            UpdateStep("等待自动门关到位10");
                    //            WaitDigital("自动门关到位");
                    //            break;
                    //        case RunStep.Move01ToCenter:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //上面相机移动到盘正中心
                    //            UpCameraMove01(distance0, distance1, velMax0, acc0, dec0, 1);
                    //            UpdateStep("上面相机移动到刹车盘中心");
                    //            //等待1、2轴停止
                    //            JudgeAxisStop(0, 1);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //上面相机拍照
                    //            PhotoUp();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move01ToGroove:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //上面相机移动到挖槽
                    //            UpCameraMove01(distance0, distance1 + 5000, velMax0, acc0, dec0, 1);
                    //            UpdateStep("上面相机移动到挖槽");
                    //            //等待1、2轴停止
                    //            JudgeAxisStop(0, 1);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //上面相机拍照
                    //            PhotoUp();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move345ToLarge:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //侧面相机移动到大外圆
                    //            SideCameraMove234(distance2, distance3, distance4, velMax2, acc2, dec2, 1);
                    //            //等待2，3，4轴停止
                    //            JudgeAxisStop(2, 3, 4);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("侧面相机移动到大外圆");
                    //            //侧面光源上升
                    //            LightUp();
                    //            UpdateStep("等待光源上升到位17");
                    //            WaitDigital("光源上升到位");
                    //            //侧面光源打开
                    //            SideLightOpen();
                    //            //侧面相机拍照
                    //            PhotoSide();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move6Rotate1:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("刹车盘旋转，同时拍挖槽和大外圆");
                    //            for (int i = 0; i < 7; i++)
                    //            {
                    //                //旋转轴旋转一定角度
                    //                MulRotateAxis5(360 / 7, velMax5, velMin5, acc5, dec5, sramp5);
                    //                //侧面光源关闭
                    //                SideLightClose();
                    //                //等待旋转轴停止
                    //                JudgeAxisStop(5);
                    //                if (IsAutoStop)
                    //                {
                    //                    fOver = true;
                    //                    break;
                    //                }
                    //                //上面相机拍照
                    //                PhotoUp();
                    //                Delay(1000);
                    //                //侧面光源打开
                    //                SideLightOpen();
                    //                //侧面光源拍照
                    //                PhotoSide();
                    //            }
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            break;
                    //        case RunStep.Move345ToSmall:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //侧面相机移动到小端侧壁
                    //            SideCameraMove234(distance2, distance3 + 500, distance4, velMax2, acc2, dec2, 1);
                    //            UpdateStep("侧面相机移动到小端侧壁");
                    //            //等待 2，3，4轴停止
                    //            JudgeAxisStop(2, 3, 4);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //侧面相机拍照
                    //            PhotoSide();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move6Rotate2:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("刹车盘旋转，拍小端侧壁");
                    //            for (int i = 0; i < 7; i++)
                    //            {
                    //                //旋转轴旋转一定角度
                    //                MulRotateAxis5(-360 / 7, velMax5, velMin5, acc5, dec5, sramp5);
                    //                //等待旋转轴停止
                    //                JudgeAxisStop(5);
                    //                if (IsAutoStop)
                    //                {
                    //                    fOver = true;
                    //                    break;
                    //                }
                    //                //侧面相机拍照
                    //                PhotoSide();
                    //                Delay(1000);
                    //            }
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //侧面光源关闭
                    //            SideLightClose();
                    //            //侧面光源下降
                    //            LightDown();
                    //            UpdateStep("等待光源下降到位18");
                    //            WaitDigital("光源下降到位");
                    //            //电磁铁下降
                    //            MagDown();
                    //            UpdateStep("等待磁铁下降到位14");
                    //            WaitDigital("磁铁下降到位");
                    //            break;
                    //        case RunStep.Move01ToSafe:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpCameraMove01(0, 0, velMax0, acc0, dec0, 1);
                    //            UpdateStep("上面相机移动到安全位置");
                    //            JudgeAxisStop(0, 1);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("等待上面相机移到安全位置19");
                    //            WaitDigital("上面相机安全位置");
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move7ToUp:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("抱紧刹车盘上升");
                    //            UpdownAxis6(distance6 + 50, velMax6, velMin6, acc6, dec6, sramp6);
                    //            JudgeAxisStop(6);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            CrampClose();
                    //            Delay(2000);
                    //            UpdownAxis6(distance6 + 500, velMax6, velMin6, acc6, dec6, sramp6);
                    //            JudgeAxisStop(6);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move8Flip:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("翻转刹车盘");
                    //            FlipAxis7(180, velMax7, velMin7, acc7, dec7, sramp7);
                    //            JudgeAxisStop(7);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("等待刹车盘正转到位15");
                    //            WaitDigital("翻转轴正转到位");
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move7ToDown:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("刹车盘翻转完毕下降");
                    //            UpdownAxis6(distance6 - 500, velMax6, velMin6, acc6, dec6, sramp6);
                    //            JudgeAxisStop(6);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            CrampOpen();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move01ToNeiQiang:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //磁铁上升
                    //            MagUp();
                    //            UpdateStep("等待磁铁上升到位13");
                    //            WaitDigital("磁铁上升到位");
                    //            UpCameraMove01(distance0, distance1 - 300, velMax0, acc0, dec0, 1);
                    //            UpdateStep("上面相机移动到内腔");
                    //            JudgeAxisStop(0, 1);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            PhotoUp();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move01234BrakeAndNeiqiang:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpCameraMove01(distance0 + 500, distance1, velMax0, acc0, dec0, 1);
                    //            UpdateStep("上面相机移动到刹面");
                    //            JudgeAxisStop(0, 1);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            SideCameraMove234(distance2 + 500, distance3, distance4 + 45, velMax2, acc2, dec2, 1);
                    //            UpdateStep("侧面相机移动到内腔侧壁");
                    //            JudgeAxisStop(2, 3, 4);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            PhotoUp();
                    //            PhotoSide();
                    //            Delay(1000);
                    //            break;
                    //        case RunStep.Move6Rotate3:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("刹车盘旋转拍照刹面和内腔侧壁");
                    //            for (int i = 0; i < 7; i++)
                    //            {
                    //                MulRotateAxis5(360 / 7, velMax5, velMin5, acc5, dec5, sramp5);
                    //                JudgeAxisStop(5);
                    //                if (IsAutoStop)
                    //                {
                    //                    fOver = true;
                    //                    break;
                    //                }
                    //                PhotoUp();
                    //                PhotoSide();
                    //                Delay(1000);
                    //            }
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            break;
                    //        case RunStep.Move01234ToSafe:
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            //电磁铁下降
                    //            MagDown();
                    //            UpCameraMove01(0, 0, velMax0, acc0, dec0, 1);
                    //            UpdateStep("上面相机移动到安全位置");
                    //            JudgeAxisStop(0, 1);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("等待上面相机移到安全位置19");
                    //            WaitDigital("上面相机安全位置");
                    //            SideCameraMove234(0, 0, 0, velMax2, acc2, dec2, 1);
                    //            UpdateStep("侧面相机移动到安全位置");
                    //            JudgeAxisStop(2, 3, 4);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            UpdateStep("等待侧面相机移到安全位置20");
                    //            WaitDigital("侧面相机安全位置");
                    //            ABSRotateAxis5(0, velMax5, velMin5, acc5, dec5, sramp5);
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            AutoDoorOpen();
                    //            UpdateStep("等待磁铁下降到位14");
                    //            WaitDigital("磁铁下降到位");
                    //            UpdateStep("自动门开到位9");
                    //            WaitDigital("自动门开到位");
                    //            UpdateStep("流程结束");
                    //            break;

                    //        default:
                    //            break;
                    //    }


                    //    //检测是否有暂停信号

                    //    if (IsPause)
                    //    {
                    //        AddLog(0, "系统暂停中");
                    //        UpdateStep("系统暂停中");
                    //        while (IsPause && CommendMethods.bClose == false)
                    //        {
                    //            if (IsAutoStop)
                    //            {
                    //                fOver = true;
                    //                break;
                    //            }
                    //            Thread.Sleep(10);
                    //        }
                    //    }

                    //    //当前步骤加1
                    //    CurrentStep++;
                    //    //检测是否为最后一步
                    //    if (CurrentStep == RunStep.MaxStep)
                    //    {
                    //        break;
                    //    }

                    //    //检测fOver是否为true
                    //    if (fOver)
                    //    {
                    //        break;
                    //    }

                    //}

                    if (fOver)
                    {
                        if (IsAutoStop)
                        {
                            UpdateStep("紧急停止");

                        }
                        if (AutoMode == false)
                        {
                            AddLog(1, "自动运行过程中切换为手动模式，紧急停止");
                            break;
                        }
                        fOver = false;
                        IsAutoStop = false;
                        Delay(50);
                    }

                }

            }


        }

        #endregion

        #region 自动流程条件判断
        //允许机械手进入
        private bool GetAutoState()
        {
            bool result = true;
            if (ReadDI(DI["自动门开到位"]) == 0)
            {
                result = false;
                AddLog(1, "自动门未打开9");
            }
            if (ReadDI(DI["急停"]) == 0 || IsEmgStop)
            {
                result = false;
                AddLog(1, "急停按钮按下");
            }
            if (IsPause)
            {
                result = false;
                AddLog(1, "暂停按钮按下");
            }
            if (ReadDI(DI["上面相机安全位置"]) == 0)
            {
                result = false;
                AddLog(1, "上面相机不在安全位置19");
            }
            if (ReadDI(DI["侧面相机安全位置"]) == 0)
            {
                result = false;
                AddLog(1, "侧面相机不在安全位置20");
            }
            if (ReadDI(DI["光源下降到位"]) == 0)
            {
                result = false;
                AddLog(1, "光源下降不到位18");
            }
            if (ReadDI(DI["磁铁下降到位"]) == 0)
            {
                result = false;
                AddLog(1, "磁铁下降不到位14");
            }
            if (ReadDI(DI["手爪打开到位"]) == 0)
            {
                result = false;
                AddLog(1, "抱紧手爪未打开12");
            }
            if (ReadDI(DI["急停"]) == 0)
            {
                result = false;
                AddLog(1, "急停按钮未拔起0");
            }
            if (ReadDI(DI["手动门关到位1"]) == 0)
            {
                result = false;
                AddLog(1, "手动门未关闭21");
            }
            if (result)
            {
                return true;
            }
            else
            {
                return false;
            }



        }


        #endregion

        #region 通用写入日志的方法
        /// <summary>
        /// 当前系统时间，如果想改格式，直接改一个地方就可以
        /// </summary>
        private string CurrentTime
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }
        /// <summary>
        /// 通用写入日志的方法
        /// </summary>
        /// <param name="level"></param>
        /// <param name="info"></param>
        private void AddLog(int level, string info)
        {
            if (this.list_info.InvokeRequired)
            {//跨线程调用
                Invoke(new Action(() =>
                {
                    ListViewItem ist = new ListViewItem("  " + CurrentTime, level);
                    ist.SubItems.Add(info);
                    //让最新的数据在最上面
                    this.list_info.Items.Insert(0, ist);
                    //只保存5条信息
                    if (list_info.Items.Count > 20)
                    {
                        list_info.Items.RemoveAt((list_info.Items.Count - 1));
                    }
                }));
            }
            else
            {
                ListViewItem ist = new ListViewItem(" " + CurrentTime, level);
                ist.SubItems.Add(info);
                //让最新的数据在最上面
                this.list_info.Items.Insert(0, ist);
                //只保存5条信息
                if (list_info.Items.Count > 20)
                {
                    list_info.Items.RemoveAt((list_info.Items.Count - 1));
                }
            }



        }




















        #endregion

        #region 临时测试
        CancellationTokenSource cts;
        int Cyclenum = 0;
        private void btn_Testlinshi_Click(object sender, EventArgs e)
        {
            
            if (isManuTaskRunning)
            {
                MessageBox.Show("流程已开始");
                return;
            }
            isManuTaskRunning = true;
            cts = new CancellationTokenSource();
            Cyclenum = 0;
            this.check_Run.Checked = true;
            Task.Run(() =>
            {
                using (cts.Token.Register((Thread.CurrentThread.Abort)))
                {
                    while (true)
                    {
                        int a = Test();
                        Cyclenum++;
                        AddLog(0, "第" + Cyclenum + "次循环结束");
                        if (a != 1)
                        {
                            AddLog(2, "第" + Cyclenum + "次循环出故障");
                            ReadAlarm(a);
                            isManuTaskRunning = false;
                            break;
                        }
                        Delay(2000);
                    }
                }
            }, cts.Token);
        }

        private void btn_StopLinshi_Click(object sender, EventArgs e)
        {
            cts.Cancel();
            isManuTaskRunning = false;
        }

        #endregion

        private void grbMode_Enter(object sender, EventArgs e)
        {

        }

        private void btn_SystemZero_Click(object sender, EventArgs e)
        {

        }

        private void btn_Communication_Click(object sender, EventArgs e)
        {
            if (b_con1)
            {
                this.tab_Monitor.SelectedIndex = 6;
                return;
            }
            DialogResult dialogResult = new FrmPassword().ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                this.tab_Monitor.SelectedIndex = 6;
                b_con1 = true;
                Task.Run(() =>
                {
                    Delay(300000);
                    b_con1 = false;
                });
            }
        }

        private void txt_SaveMessc_Click(object sender, EventArgs e)
        {
            if (this.rdo_sanling.Checked == false && this.rdo_ximenzi.Checked == false)
            {
                MessageBox.Show("请选择要保存的PLC类型");
                return;
            }

            if (this.rdo_sanling.Checked == true)
            {
                if (SavePlcConfig())
                {
                    MessageBox.Show("保存配置成功");
                }
            }
            if (this.rdo_ximenzi.Checked == true)
            {
                if (SavePlcConfig())
                {
                    MessageBox.Show("保存配置成功");
                }
            }

        }

        private void btn_ConnMitsubichi_Click(object sender, EventArgs e)
        {
            if (plcModel == "1")
            {
                bool a = mc.Conncet(IPMitsu, PortMitsu);
                if (a)
                {
                    MessageBox.Show("三菱PLC连接成功");
                }
                else
                {
                    MessageBox.Show("三菱PLC连接失败");
                }
            }
            if (plcModel == "2")
            {
                bool a = ms.Conncet(IPMitsu);
                if (a)
                {
                    MessageBox.Show("西门子PLC连接成功");
                }
                else
                {
                    MessageBox.Show("西门子PLC连接失败");
                }
            }




        }

        private void btn_CheckOK_Click(object sender, EventArgs e)
        {
            if (plcModel == "1")
            {
                mc.WriteSingle(checkOK, true);
            }
            if (plcModel == "2")
            {
                ms.WriteSingle(checkOK, true);
            }



        }

        private void btn_Model_Click(object sender, EventArgs e)
        {
            int a = mc.ReadRegister(preFix);
            string Model = mc.DecodAskDA(a) + mc.ReadRegister(model).ToString();
            MessageBox.Show(Model);
        }

        private void btn_DisMitsu_Click(object sender, EventArgs e)
        {
            if (plcModel == "1")
            {
                mc.Disconncet();
                MessageBox.Show("三菱PLC断开成功");
            }
            if (plcModel == "2")
            {
                ms.Disconncet();
                MessageBox.Show("西门子PLC断开成功");
            }

        }
        CancellationTokenSource cts1;

        private void btn_robotIN_Click(object sender, EventArgs e)
        {
            cts1 = new CancellationTokenSource();
            if (plcModel=="1")
            {
                Task task1 = Task.Run(() =>
                {
                    using (cts1.Token.Register((Thread.CurrentThread.Abort)))
                    {
                        while (true)
                        {
                            Thread.Sleep(200);
                            if (mc.ReadSingle(robotIn) == true)
                            {
                                this.Invoke(new Action(() =>
                                {
                                    this.btn_robotIN.BackColor = Color.Red;
                                }));
                            }
                            else
                            {
                                this.Invoke(new Action(() =>
                                {
                                    this.btn_robotIN.BackColor = Color.Green;
                                }));
                            }

                        }
                    }

                }, cts1.Token);
            }
            if (plcModel == "2")
            {
                Task task1 = Task.Run(() =>
                {
                    using (cts1.Token.Register((Thread.CurrentThread.Abort)))
                    {
                        while (true)
                        {
                            Thread.Sleep(200);
                            if (ms.ReadSingle(robotIn) == true)
                            {
                                this.Invoke(new Action(() =>
                                {
                                    this.btn_robotIN.BackColor = Color.Red;
                                }));
                            }
                            else
                            {
                                this.Invoke(new Action(() =>
                                {
                                    this.btn_robotIN.BackColor = Color.Green;
                                }));
                            }

                        }
                    }

                }, cts1.Token);
            }

        }

        #region  三菱PLC方法
        /// <summary>
        /// 连接PLC
        /// </summary>
        /// <returns></returns>
        public int ConnectPLC()
        {
            return mc.Conncet(IPMitsu, PortMitsu) ? 1 : -44;
        }
        /// <summary>
        /// 断开PLC
        /// </summary>
        /// <returns></returns>
        public int DisConnnectPLC()
        {
            return mc.Disconncet() ? 1 : -45;
        }
        /// <summary>
        /// 机械手请求进入
        /// </summary>
        /// <returns>true表示机械手请求进入</returns>
        public int RobotCall()
        {
            //返回1 请求进入
            return mc.ReadSingle(robotCall) ? 1 : 0;
        }
        /// <summary>
        /// 启动设备
        /// </summary>
        /// <returns></returns>
        public int StartMachine()
        {
            //返回1，启动设备
            //启动设备的条件是机械手不在设备内
            if (mc.ReadSingle(robotIn) == true && mc.ReadSingle(startMachine) == true)
            {
                mc.WriteSingle(machineRun, true);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 设备是否准备OK
        /// </summary>
        /// <param name="value">1准备OK，其他准备不OK</param>
        public void MachineOK(int value)
        {
            if (value == 1)
            {
                mc.WriteSingle(machineOK, true);
            }
            else
            {
                mc.WriteSingle(machineOK, false);
            }
        }

        /// <summary>
        /// 返回检测结果
        /// </summary>
        /// <param name="checkValue">1检测OK，其他不OK</param>
        /// <param name="posValue">1位置度OK，其他不OK</param>
        public void CheckResult(int checkValue, int posValue)
        {
            mc.WriteSingle(machineRun, false);
            if (checkValue == 1)
            {
                mc.WriteSingle(checkOK, true);
                mc.WriteSingle(checkNG, false);
            }
            else
            {
                mc.WriteSingle(checkOK, false);
                mc.WriteSingle(checkNG, true);
            }

            if (posValue == 1)
            {
                mc.WriteSingle(posOK, true);
                mc.WriteSingle(posNG, false);
            }
            else
            {
                mc.WriteSingle(posOK, false);
                mc.WriteSingle(posNG, true);
            }

        }

        private void SysReset()
        {
            mc.WriteSingle(machineOK, false);
            mc.WriteSingle(checkOK, false);
            mc.WriteSingle(checkNG, false);
            mc.WriteSingle(posOK, false);
            mc.WriteSingle(posNG, false);
        }

        private void WriteHeart()
        {
            mc.WriteSingle(Heart, true);
        }

        //设备已启动
        private void MachineRun(int value)
        {
            if (value == 1)
            {
                mc.WriteSingle(machineRun, true);
            }
            if (value == 0)
            {
                mc.WriteSingle(machineRun, false);
            }
        }




        //判断机械手是否已进入未写



        #endregion


        private void panel_Multy_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btn_Down0_Click_1(object sender, EventArgs e)
        {

        }
    }









}
